企业库ExceptionManager:“日志条目字符串太长。”

时间:2009-08-26 14:21:24

标签: .net exception-handling enterprise-library

我们使用Microsoft的Enterprise Library(4.1)并且经常遇到以下问题:

[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
...

不幸的是DefaultPublisher的{​​{1}}方法在写入EventLog之前没有进行任何边界检查,所以我们原来的Stacktrace丢失了。

有没有办法解决这个问题(最好通过配置)并仍然使用WriteToLog

1 个答案:

答案 0 :(得分:1)

由于您的消息太长,您无法登录EventLog(但您已经知道了!)。我认为你的三个选择是:

  1. 将所有日志记录更改为写入没有实际大小限制的目标(例如,平面文件)
  2. 将消息记录到EventLog,但重定向发生在没有实际大小限制的目标上的任何错误(例如,平面文件)
  3. 修复企业库的“问题”
  4. 选项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,并且发生的任何错误(包括字符串太长的异常)都将写入文件。这是一个痛苦,因为您的日志现在跨越两个单独的数据源。
    • 错误日志的格式与成功的日志消息的格式不同,这可能会使解析日志变得困难。
    • 当您尝试记录太长的字符串时会导致性能下降,因为会抛出,捕获并处理另一个异常。