Which code segment should you use?

You are testing a newly developed method named PersistToDB. This method accepts a parameter of type EventLogEntry.
This method does not return a value. You need to create a code segment that helps you to test the method.
The code segment must read entries from the application log of local computers and then pass the entries on to the PersistToDB method.
The code block must pass only events of type Error or Warning from the source MySource to the PersistToDB method.
Which code segment should you use?

You are testing a newly developed method named PersistToDB. This method accepts a parameter of type EventLogEntry.
This method does not return a value. You need to create a code segment that helps you to test the method.
The code segment must read entries from the application log of local computers and then pass the entries on to the PersistToDB method.
The code block must pass only events of type Error or Warning from the source MySource to the PersistToDB method.
Which code segment should you use?

A.
EventLog myLog = new EventLog(“Application”,”.”);
foreach (EventLogEntry entry in myLog.Entries){
if (entry.Source == “MySource”){
PersistToDB(entry);
}
}

B.
EventLog myLog = new EventLog(“Application”,”.”);
myLog.Source = “MySource”;
foreach (EventLogEntry entry in myLog.Entries){
if (entry.EntryType == (EventLogEntryType.Error & EventLogEntryType.Warning)){
PersistToDB(entry);
}
}

C.
EventLog myLog = new EventLog(“Application”,”.”);
foreach (EventLogEntry entry in myLog.Entries){
if (entry.Source == “MySource”){
if (entry.EntryType == EventLogEntryType.Error || entry.EntryType == EventLogEntryType.Warning){
PersistToDB(entry);
}
}
}

D.
EventLog myLog = new EventLog(“Application”,”.”);
myLog.Source = “MySource”;
foreach (EventLogEntry entry in myLog.Entries){
if (entry.EntryType == EventLogEntryType.Error || entry.EntryType == EventLogEntryType.Warning){
PersistToDB(entry);
}
}

Explanation:
It is necessary to create a new Application EventLog, iterate over all the EventLogEntries and call the PersistToDB method
if the entry is a warning or error and the source is MySource.
A will PersistToDb irrespective of the type of log entry. The question explicitly states only warnings and errors should be persisted.
B features an incorrect test for warnings and errors.
D&B do not ensure that only MySource entries are persisted. Instead they overwrite the source.



Leave a Reply 1

Your email address will not be published. Required fields are marked *