Logging and exceptions in one file, only exceptions in another

Topics: Exception Handling Application Block, Logging Application Block
Jun 9, 2010 at 11:33 AM

Is it possible to configure the Enterprise Library in such a way that all the logging statements and exceptions are written to one file, for instance all.txt, and then all the exceptions to a separate file named errors.txt?

Also, is it possible to use the handlinginstanceid as the filename for the separate file which will be created for each exception?

Thanks.

Jun 10, 2010 at 7:19 AM

Yes this is possible, configuring the Logging block to use a single flat file for all categories that you may want the logentry to be logged is what you need to do this. Same with Exception logging since technically it also uses the logging block for its Logging Handler.

As for naming your exception log filename with HandlingInstance this can be done programmatically and here are steps you need to do to achieve this.

  1. Retrieve the HandlingInstanceId for the specific exception thrown upon execution of the ExceptionPolicy.HandleException . The only way I know how you can retrieve the HandlingInstanceId value is thru a custom exception handler so you will need to create your own custom exception handler. see documentation http://msdn.microsoft.com/en-us/library/ff664570(v=PandP.50).aspx on how to create a custom exception handler.
  2. Inside your Custom Exception Handler  "public Exception HandleException(Exception exception, Guid handlingInstanceId)" method, utilize the handlingInstanceId argument parameter from there to dynamically renaming your Logging Flat File Trace Listener to your desired naming format. You can do this by putting something like this code. 

            Configuration xmlConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            LoggingSettings setting = (LoggingSettings)xmlConfiguration.GetSection(LoggingSettings.SectionName);

            FlatFileTraceListenerData flatFile = setting.TraceListeners.Get("Flat File Trace Listener") as FlatFileTraceListenerData;
            flatFile.FileName = "C:\Avanade\logs\" + handlingInstanceId + "trace.log";
           
            xmlConfiguration.Save();
            ConfigurationManager.RefreshSection(LoggingSettings.SectionName);

Then, that's it. You should now be able to log an exception with a flat file filename according to the HandlingInstanceId of the exception thrown.

For some references that might help please see the following

http://entlib.codeplex.com/Thread/View.aspx?ThreadId=42926 - retrieving HandlingInstanceId

http://entlib.codeplex.com/Thread/View.aspx?ThreadId=213094 - dynamically renaming log file name

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Jun 10, 2010 at 8:38 AM

Thanks for the reply.

Unfortunately i can't get it to log the errors in the file all.txt no matter what i try. Can you please give advice on what i need to change in the configuration below to get it to log the errors in the all.txt file?

<configuration>
    <configSections>
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
        <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <loggingConfiguration name="" tracingEnabled="false" defaultCategory="All"
        revertImpersonation="false">
        <listeners>
            <add name="Errors Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                fileName="Logs\Errors.txt" formatter="Errors formatter" rollInterval="Midnight"
                maxArchivedFiles="7" filter="Warning" />
            <add name="All Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                fileName="Logs\All.txt" footer="" formatter="All Formatter"
                header="" rollInterval="Midnight" maxArchivedFiles="7" />
        </listeners>
        <formatters>
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                template="Timestamp: {timestamp(local:dd.MM.yyyy hh:mm:ss.fff)}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
                name="Errors formatter" />
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                template="{timestamp(local:dd.MM.yyyy hh:mm:ss.fff)} - {severity} - {message}"
                name="All Formatter" />
        </formatters>
        <logFilters>
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                enabled="true" name="Logging Enabled Filter" />
        </logFilters>
        <categorySources>
            <add switchValue="All" name="All">
                <listeners>
                    <add name="All Listener" />
                </listeners>
            </add>
            <add switchValue="Warning" name="Errors">
                <listeners>
                    <add name="Errors Listener" />
                    <add name="All Listener" />
                </listeners>
            </add>
        </categorySources>
        <specialSources>
            <allEvents switchValue="Off" name="All Events" />
            <notProcessed switchValue="Off" name="Unprocessed Category" />
            <errors switchValue="Off" name="Logging Errors &amp; Warnings" />
        </specialSources>
    </loggingConfiguration>
    <exceptionHandling>
        <exceptionPolicies>
            <add name="Default">
                <exceptionTypes>
                    <add name="All Exceptions" type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                        postHandlingAction="None">
                        <exceptionHandlers>
                            <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                                logCategory="Errors" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
                                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                                priority="0" />
                        </exceptionHandlers>
                    </add>
                </exceptionTypes>
            </add>
        </exceptionPolicies>
    </exceptionHandling>
</configuration>

Jun 10, 2010 at 1:39 PM

Try setting the switchValue/Minimumm Severity  of your "Errors" Category to "All" <add switchValue="Warning" name="All" > .

Its kind a look like a bug on the Category Source level filter but I’m still not sure if it is indeed or just by design. Upon trying this with the 4.1 version it just behave the same with 5. Though in the table of the Severity (SourceLevels) Values in the documentation (http://msdn.microsoft.com/en-us/library/ff664426(v=PandP.50).aspx) it is quite confusing.

Also, I think someone have encountered this problem after I found this issue logged in the tracker http://entlib.codeplex.com/workitem/23145 . I’ll get back to you on this once I have confirmed what’s the real deal about this.

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com