2009-08-26 42 views
0

我們使用微軟的Enterprise Library(4.1),並經常有以下問題:企業庫ExceptionManager:「日誌條目字符串太長。」

[ArgumentException: Log entry string is too long. A string written to the event log cannot exceed 32766 characters.] 
    System.Diagnostics.EventLog.InternalWriteEvent(UInt32 eventID, UInt16 category, EventLogEntryType type, String[] strings, Byte[] rawData, String currentMachineName) +1005489 
    System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) +264 
    System.Diagnostics.EventLog.WriteEntry(String source, String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) +87 
    System.Diagnostics.EventLog.WriteEntry(String source, String message, EventLogEntryType type) +14 
    Microsoft.ApplicationBlocks.ExceptionManagement.DefaultPublisher.WriteToLog(String entry, EventLogEntryType type) in D:\temp\Exception Management\Code\CS\Microsoft.ApplicationBlocks.ExceptionManagement\ExceptionManager.cs:647 
    Microsoft.ApplicationBlocks.ExceptionManagement.DefaultPublisher.Publish(Exception exception, NameValueCollection additionalInfo, NameValueCollection configSettings) in D:\temp\Exception Management\Code\CS\Microsoft.ApplicationBlocks.ExceptionManagement\ExceptionManager.cs:634 
    Microsoft.ApplicationBlocks.ExceptionManagement.ExceptionManager.PublishToDefaultPublisher(Exception exception, NameValueCollection additionalInfo) in D:\temp\Exception Management\Code\CS\Microsoft.ApplicationBlocks.ExceptionManagement\ExceptionManager.cs:287 
    Microsoft.ApplicationBlocks.ExceptionManagement.ExceptionManager.Publish(Exception exception, NameValueCollection additionalInfo) in D:\temp\Exception Management\Code\CS\Microsoft.ApplicationBlocks.ExceptionManagement\ExceptionManager.cs:232 
    Microsoft.ApplicationBlocks.ExceptionManagement.ExceptionManager.Publish(Exception exception) in D:\temp\Exception Management\Code\CS\Microsoft.ApplicationBlocks.ExceptionManagement\ExceptionManager.cs:66 
... 

不幸的是,DefaultPublisherWriteToLog方法不執行任何邊界寫入事件日誌所以我們原來的堆棧跟蹤丟失前檢查。

有沒有辦法解決這個問題(最好是通過配置)並仍然使用DefaultPublisher

回答

1

由於您的消息太長,您無法登錄到EventLog(但您已經知道了!)。我覺得你有三個選項:

  1. 更改所有日誌記錄寫入到一個目的地不具有實際尺寸限制(如平面文件)
  2. 將消息記錄到事件日誌,但重定向發生的任何錯誤到目的地不具有實際尺寸限制(如平面文件)
  3. 修復「問題」與企業庫

選項1和2可以通過配置來完成。要支持選項2,您需要在LoggingConfiguration部分中配置錯誤目標。它看起來類似如下:

<specialSources> 
    <errors name="errors" switchValue="All"> 
    <listeners> 
     <add name="Flat File Destination"/> 
    </listeners> 
    </errors> 
</specialSources> 
<listeners> 
    <add name="Flat File Destination" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging" fileName ="loggerErrors.log" /> 
</listeners> 



我更傾向於將與方案1走,因爲選項2有一定的負面影響。選項2的一些否定內容如下:

  • 所有成功的日誌寫入將被定向到EventLog,並且發生的任何錯誤(包括字符串太長的異常)都將寫入文件。由於您的日誌現在跨越兩個獨立的數據源,這是一種痛苦。
  • 錯誤日誌格式與成功的日誌消息格式不同,這些日誌消息可能會使日誌解析困難。
  • 當您嘗試記錄太長的字符串時性能降低,因爲會引發並處理另一個異常。
相關問題