代码之家  ›  专栏  ›  技术社区  ›  Josef Pfleger

企业库异常管理器:“日志条目字符串太长。”

  •  0
  • Josef Pfleger  · 技术社区  · 15 年前

    我们使用微软的 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 WriteToLog 方法在写入事件日志之前不执行任何边界检查,因此原始StackTrace将丢失。

    有没有办法解决这个问题(最好是通过配置),并且仍然使用 默认发布服务器 ?

    1 回复  |  直到 15 年前
        1
  •  1
  •   Randy Levy    15 年前

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

    1. 更改所有日志以写入没有实际大小限制的目标(例如平面文件)
    2. 将消息记录到事件日志,但将发生的任何错误重定向到没有实际大小限制的目标(例如平面文件)
    3. 解决企业库的“问题”

    选项1和2可以通过配置完成。要支持选项2,您需要在日志配置部分配置一个错误目标。它将类似于以下内容:

     <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的一些否定词是:

    • 所有成功的日志写入都将指向事件日志,发生的任何错误(包括字符串太长异常)都将写入文件。这是一个难题,因为您的日志现在跨越了两个独立的数据源。
    • 错误日志的格式与成功日志消息的格式不同,这可能会使分析日志变得困难。
    • 当您试图记录一个太长的字符串时会受到性能惩罚,因为另一个异常将被抛出、捕获和处理。