Exception handling and logging

Topics: Exception Handling Application Block, Logging Application Block
Apr 5, 2009 at 8:56 PM
I am fairly new to Enterprise Library and trying to get my exceptions to log to individual files. I have useDefaultLogger="false", but every exception gets written to the same file. Here is my .config file so far, what am I missing? 

  <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="Information" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add fileName=".\Logs\Exceptions\exception.log" header=""
        footer="" formatter="Trace Formatter"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
        name="Exception TraceListener" />
      <add fileName=".\Logs\trace.log" footer="" formatter="Trace Formatter"
        header="" rollFileExistsBehavior="Overwrite" rollInterval="Midnight"
        rollSizeKB="0" timeStampPattern="yyyyMMdd" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
        name="Trace Listener" />
    </listeners>
    <formatters>
      <add template="[{timestamp}][Severity:{severity}] - {message}"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
        name="Trace Formatter" />
    </formatters>
    <logFilters>
      <add enabled="true" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
        name="LogEnabled Filter" />
    </logFilters>
    <categorySources>
      <add switchValue="All" name="Exception">
        <listeners>
          <add name="Exception TraceListener" />
        </listeners>
      </add>
      <add switchValue="Error" name="Error">
        <listeners>
          <add name="Trace Listener" />
        </listeners>
      </add>
      <add switchValue="Information" name="Information">
        <listeners>
          <add name="Trace Listener" />
        </listeners>
      </add>
      <add switchValue="Warning" name="Warning">
        <listeners>
          <add name="Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>

  <exceptionHandling>
    <exceptionPolicies>
      <add name="DefaultPolicy">
        <exceptionTypes>
          <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow" name="Exception">
            <exceptionHandlers>
              <add logCategory="Exception" eventId="100" severity="Error" title="Exception"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
                priority="0" useDefaultLogger="false" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
                name="Logging Handler" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>
Apr 6, 2009 at 10:15 AM
Hi,

First, I just want to ask what do you mean by "logging the exception to individual files?", do you mean that for each exception that is being handled by the EHAB you want it to be logged to a brand new file? 

Valiant Dudan
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Apr 6, 2009 at 12:16 PM
Hello,

You are correct, that is the behavior I am looking for. Based on the documentation for the useDefaultLogger setting:

"This setting determines of the logging handler will write exception details to a default log file or to separate files with GUIDs as the file names. The default is False, which means it uses GUID-named files. When set to True, the handler writes every log event and exception to the same file."

That is what I am looking for, however, I have useDefaultLogger set to false, but all the exceptions are writing to the same file.
Apr 6, 2009 at 5:32 PM
As far as I know, a scenario that the "UseDefaultLogger" is useful and will behave as you've described is when you flatfiletracelistener is shared. One way to reproduce this is to have a single flatfile tracelistener. then call the Logger.Write() using the flatfiletracelistener and have a call to handle exception. the call to the Logger.Write() will remain the logfile of the tracelistener open, so when the time you call the logging handler, it will now create the file prefixed with Guid. Currently, i cant seem to find a quick workaround for this and if i got one, i get back to you.

Please see this links:
http://entlib.codeplex.com/Thread/View.aspx?ThreadId=17354
http://www.codeplex.com/entlib/Thread/View.aspx?ThreadId=29703

Valiant Dudan
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Apr 7, 2009 at 12:20 AM
Thanks for the response and the links, I see what is going on now. I was thinking the additional files was something handled by the EHAB and not the .net trace listeners themselves. I am not a fan of the GUIDs and don't plan to actually use that functionality in production, was just experimenting with the library.