Josef Pfleger
Josef Pfleger

Reputation: 74527

Enterprise Library ExceptionManager: "Log entry string is too long."

We use Microsoft's Enterprise Library (4.1) and frequently have the following problem:

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

Unfortunately the DefaultPublisher's WriteToLog method doesn't do any bounds checking before writing to the EventLog so our original Stacktrace is lost.

Is there a way to work around this (preferably by configuration) and still use the DefaultPublisher?

Upvotes: 0

Views: 4466

Answers (1)

Randy Levy
Randy Levy

Reputation: 22655

Since your message is too long you cannot log to the EventLog (but you knew that already!). I think your three options are:

  1. Change all of your logging to write to a destination that does not have a practical size limit (e.g. Flat File)
  2. Log messages to the EventLog but redirect any errors that occur to a destination that does not have a practical size limit (e.g. Flat File)
  3. Fix the "issue" with Enterprise Library

Option 1 and 2 can be done via configuration. To support option 2, you will need to configure an error destination in your LoggingConfiguration section. It will look similar to the following:

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



My preference would be to go with option 1 since option 2 has some negatives. Some of the negatives of option 2 are:

  • All of your successful log writes will be directed to the EventLog and any errors that occur (including the string too long exception) will be written to file. This is a pain since your logs now span two separate data sources.
  • The error logs are in a different format than the successful log messages which could make parsing the logs difficult.
  • A performance penalty when you attempt to log a string that is too long because another exception will be thrown, caught and handled.

Upvotes: 2

Related Questions