2011-04-08 57 views
1

遇到一個奇怪的問題,與此事件,事件包裝和處理程序:問題有異常,但沒有參數

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); 
    } 
} 

一個時期的幾分鐘內,_logger停止日誌消息,而不是我收到的這些信息事件日誌:

無法記錄狀態更新事件:System.NullReferenceException:未將對象引用設置爲對象的實例。 在ScannedService.scanDocProcessor_StatusUpdate(字符串消息,異常EXC,SeverityLevel嚴重性) 在ScannedService.Processor.FireStatusUpdate(字符串消息) 在ScannedService.Processor.ProcessQueue(對象對象)

我很困惑如何事件當它應該寫入exc.ToString()時,日誌可能會得到這樣的堆棧跟蹤。我查看了IL的scanDocProcessor_StatusUpdate方法沒有初始化一個Exception對象。除此之外,我不知道nullreferenceexception是如何拋出。當Log方法確實捕獲到一個異常時,它會吞下它,或者用「throw」重新拋出異常。 message參數永遠不爲null,SeverityLevel是一個枚舉。

+0

永遠趕不上'Exception'除非你打算退出程序。 – 2011-04-08 17:41:43

回答

0

您正在拋出異常,其中ecx爲空的else條件之一。在catch塊中,假設ecx不爲空,這會生成另一個異常,它隱藏了原始的一個。

你可以讓你的日誌語句空安全具有如下:

EventLog.WriteEntry("Russia OCR Submission Processor", 
    String.Format("Could not log status update event: {0}", exc), EventLogEntryType.Information);