Encountered a strange problem with this event, event wrapper and handler:
public delegate void StatusUpdateHandler(string message, Exception exc, SeverityLevel severity);
public event StatusUpdateHandler StatusUpdate;
private void FireStatusUpdate(string message)
{
if (this.StatusUpdate != null)
this.StatusUpdate(message, null, SeverityLevel.None);
}
void scanDocProcessor_StatusUpdate(string message, Exception exc, SeverityLevel severity)
{
try
{
if (exc != null)
{
if (severity >= setSevLevel)
this._logger.Log(message + Environment.NewLine + exc.ToString(), LogEntryType.Emergency, "OCR Submission Processor Status Update", true);
else
this._logger.Log(message + Environment.NewLine + exc.ToString(), LogEntryType.Error, "OCR Submission Processor Status Update", false);
}
else if (severity >= setSevLevel)
{
this._logger.Log(message, LogEntryType.Info, "OCR Submission Processor Status Update", true, true);
}
else
this._logger.Log(message, LogEntryType.Info, "OCR Submission Processor Status Update", false);
}
catch (Exception)
{
EventLog.WriteEntry("Russia OCR Submission Processor", "Could not log status update event: " + exc.ToString(), EventLogEntryType.Information);
}
}
For a period of a few minutes, the _logger stopped logging messages and instead I received these messages in the event log:
Could not log status update event: System.NullReferenceException: Object reference not set to an instance of an object.
at ScannedService.scanDocProcessor_StatusUpdate(String message, Exception exc, SeverityLevel severity)
at ScannedService.Processor.FireStatusUpdate(String message)
at ScannedService.Processor.ProcessQueue(Object Object)
I’m confused how the Event log could get such a stack trace when it should be writing exc.ToString(). I looked at the IL for the scanDocProcessor_StatusUpdate method is not initializing an Exception object. Beyond that I don’t know how a nullreferenceexception is getting thrown. When the Log method does catch an exception it swallows it or re-throws it with “throw;”. The message parameter is never null and SeverityLevel is an enumeration.
You are throwing an exception in one of the
elseconditions whereecxis null. In thecatchblock, you are assuming thatecxis not null, which generates another exception that hides your original one.You can make your log statement null-safe with the following: