Multiple Rolling Flat File Trace Listeners

Topics: Building and extending application blocks, Logging Application Block
Sep 23, 2010 at 4:15 AM

Hi Following is my configuration. I'd like to know how to use multiple output files. Ive created two Rolling Flat File Trace Listeners and Id like to (ideally) create a class which wraps around EL. I can then point a overloaded method to write out to either file 1 or file 2.

Thanks,

 

<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" />    </configSections>    <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">        <listeners>            <add name="Rolling Flat File Trace 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="C:\logs\rolling.txt" formatter="Text Formatter"                rollFileExistsBehavior="Increment" rollInterval="Midnight"                rollSizeKB="10000" maxArchivedFiles="100" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack" />            <add name="Rolling Flat File Trace Listener 2" 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="C:\logs\rolling2.txt" formatter="Text Formatter"                rollInterval="Midnight" rollSizeKB="10000" maxArchivedFiles="100"                traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack" />        </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}{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="Text Formatter" />        </formatters>        <categorySources>            <add switchValue="All" name="General">                <listeners>                    <add name="Rolling Flat File Trace Listener" />                </listeners>            </add>            <add switchValue="All" name="Category">                <listeners>                    <add name="Rolling Flat File Trace Listener" />                </listeners>            </add>        </categorySources>        <specialSources>            <allEvents switchValue="All" name="All Events" />            <notProcessed switchValue="All" name="Unprocessed Category">                <listeners>                    <add name="Rolling Flat File Trace Listener" />                </listeners>            </notProcessed>            <errors switchValue="All" name="Logging Errors &amp; Warnings">                <listeners>                    <add name="Rolling Flat File Trace Listener" />                </listeners>            </errors>        </specialSources>    </loggingConfiguration></configuration>

Sep 23, 2010 at 3:41 PM

If you want to write to 2 or more files, you can either create a category and add the trace listeners you want under it.  If you want to log either to only one of them at any time, create separate categories for each.  Use the name of category to distinguish which trace listener you want to use.   In your config, you defined two categories : General and Category.  However, you referenced the same rolling trace listener so logging to the General or Category category will result to logging to the same file which is rolling.txt.  To log to rolling2.txt, one of your categories should reference Rolling Flat File Trace Listener 2.  After correcting your configuration, your code should look like this:

LogWriter logWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

logWriter.Write("test message", "General");  //this logs using the trace listener specified under the "General" category.

logWriter.Write("test message 2", "Category"); //this logs using the trace listener specified under the "Category" category

Does this answer your question?

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
   

Sep 23, 2010 at 9:27 PM

That works well Sarah. Thanks.