How to add RollingFlatFileTraceListenerData programmatically

Topics: Logging Application Block
Sep 9, 2012 at 9:53 AM

I have a config file like:

<configSections> 
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> 
</configSections> 
 
<loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="Tracing" logWarningsWhenNoCategoriesMatch="true"> 
    <listeners> 
        <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.SystemDiagnosticsTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" type="System.Diagnostics.ConsoleTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="System Diagnostics Trace Listener"/> 
    </listeners> 
    <formatters> 
        <add template="{message}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Text Formatter"/> 
    </formatters> 
    <categorySources> 
          <add switchValue="All" name="AppLog"> 
    <listeners> 
          <add name="System Diagnostics Trace Listener"/> 
    </listeners> 
  </add> 
</categorySources> 
<specialSources> 
  <allEvents switchValue="All" name="All Events"/>       
  <notProcessed switchValue="All" name="Unprocessed Category"/> 
  <errors switchValue="Off" name="Logging Errors &amp; Warnings"/> 
</specialSources> 

 

Besides the console listener that I have, I want to define a RollingFlatFileTraceListenerData programmatically:

var listener = new RollingFlatFileTraceListenerData("AppLog", @"c:\log.log", "", "", 0, "yyyyMMdd-hhmm", Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollFileExistsBehavior.Increment, Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollInterval.Hour, TraceOptions.LogicalOperationStack, "Text Formatter"); 

How can I add my newly defined listener to the list of listeners programmatically?

Sep 9, 2012 at 3:08 PM

The preferred approach (it's so much easier!) to programmatic configuration is to use the Fluent Interface (see The Logging Block Section.  

A sample for RollingFlatFileTraceListener would look like this:

    var builder = new ConfigurationSourceBuilder();

    builder.ConfigureLogging()
            .WithOptions
                .DoNotRevertImpersonation()
            .LogToCategoryNamed("General")
                .WithOptions
                .SetAsDefaultCategory()
                .SendTo.RollingFile("Rolling Log File")
                .FormatWith(new FormatterBuilder()
                    .TextFormatterNamed("Text Formatter")
                    .UsingTemplate("Message: {message} Timestamp: {timestamp}...{newline})}"))
                    .ToFile("rolling.log")
                    .RollAfterSize(1024)
                    .RollEvery(Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollInterval.Day)
                    .WhenRollFileExists(Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollFileExistsBehavior.Overwrite)
                    .UseTimeStampPattern("MM-dd-yyyy")
                    .WithHeader("------------------------------")
                    .WithTraceOptions(TraceOptions.Callstack | TraceOptions.DateTime | TraceOptions.LogicalOperationStack | TraceOptions.ProcessId | TraceOptions.ThreadId | TraceOptions.Timestamp)
                    .CleanUpArchivedFilesWhenMoreThan(2);

    var configSource = new DictionaryConfigurationSource();
    builder.UpdateConfigurationWithReplace(configSource);
    EnterpriseLibraryContainer.Current
        = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com