Switch listeners and priority filters while runtime without application recycle

Topics: Logging Application Block
Jul 15, 2014 at 6:15 PM
Hi , We are using Enterprise Library Logging Application Block 6.0 for logging application events and message . We are now looking for two setting options

1.) We have to change the Priority Filter at run-time
2.)We need to turn on/off Event Log Listener, Database Trace Listener (already configure in the config file)

Configuration change should not reset the application pool.

Below is logged registration code
DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());
IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
Logger.SetLogWriter(logWriterFactory.Create());
Note : We have loggingConfiguration in a separate config file
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
    <listeners>
      <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        source="Enterprise Library Logging" formatter="Text Formatter"
        log="" machineName="." traceOutputOptions="None" asynchronous="true" />
      <add name="Exception Handling Database Trace Listener" type="Ika.Framework.Logging.Exceptions.DB.OracleTraceListener, Ika.Framework.Logging"
        listenerDataType="Ika.Framework.Logging.Exceptions.DB.OracleTraceListenerData, Ika.Framework.Logging"
        databaseInstanceName="ErrorLogDBConnection" writeLogStoredProcName="PKG_EL_LOGGER.WRITEEXCEPTIONLOG"
        addCategoryStoredProcName="PKG_EL_LOGGER.ADDEXCEPTIONCATEGORY"
        asynchronous="true" />
      <add name="Instrumentation Logging Database Trace Listener" type="Ika.Framework.Logging.DB.OracleTraceListener, Ika.Framework.Logging"
        listenerDataType="Ika.Framework.Logging.DB.OracleTraceListenerData, Ika.Framework.Logging"
        databaseInstanceName="ErrorLogDBConnection" writeLogStoredProcName="PKG_EL_LOGGER.WRITELOG"
        addCategoryStoredProcName="PKG_EL_LOGGER.ADDCATEGORY" formatter="Instrumentation DB  Formatter"
        asynchronous="true" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.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" />
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        template="{message}{priority}" name="Instrumentation DB  Formatter" />
    </formatters>
    <logFilters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.PriorityFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        minimumPriority="1" maximumPriority="2" name="Priority Filter" />
    </logFilters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Event Log Listener" />
          <add name="Exception Handling Database Trace Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="InstrumentationLoggingCategorySource">
        <listeners>
          <add name="Event Log Listener" />
          <add name="Instrumentation Logging Database 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="Event Log Listener" />
          <add name="Exception Handling Database Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
Jul 16, 2014 at 3:26 AM
Edited Jul 16, 2014 at 3:28 AM
There are a variety of ways you could go about it.

Since you already have the logging configuration in a separate file I would probably load that configuration using FileConfigurationSource (which will let you watch for configuration changed event) and then reconfigure.

logging.config file would look something like:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
    <listeners>
      <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        source="Enterprise Library Logging" formatter="Text Formatter"
        log="" machineName="." traceOutputOptions="None" asynchronous="true" />
      <add name="Exception Handling Database Trace Listener" type="Ika.Framework.Logging.Exceptions.DB.OracleTraceListener, Ika.Framework.Logging"
        listenerDataType="Ika.Framework.Logging.Exceptions.DB.OracleTraceListenerData, Ika.Framework.Logging"
        databaseInstanceName="ErrorLogDBConnection" writeLogStoredProcName="PKG_EL_LOGGER.WRITEEXCEPTIONLOG"
        addCategoryStoredProcName="PKG_EL_LOGGER.ADDEXCEPTIONCATEGORY"
        asynchronous="true" />
      <add name="Instrumentation Logging Database Trace Listener" type="Ika.Framework.Logging.DB.OracleTraceListener, Ika.Framework.Logging"
        listenerDataType="Ika.Framework.Logging.DB.OracleTraceListenerData, Ika.Framework.Logging"
        databaseInstanceName="ErrorLogDBConnection" writeLogStoredProcName="PKG_EL_LOGGER.WRITELOG"
        addCategoryStoredProcName="PKG_EL_LOGGER.ADDCATEGORY" formatter="Instrumentation DB  Formatter"
        asynchronous="true" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.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" />
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        template="{message}{priority}" name="Instrumentation DB  Formatter" />
    </formatters>
    <logFilters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.PriorityFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        minimumPriority="1" maximumPriority="2" name="Priority Filter" />
    </logFilters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Event Log Listener" />
          <add name="Exception Handling Database Trace Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="InstrumentationLoggingCategorySource">
        <listeners>
          <add name="Event Log Listener" />
          <add name="Instrumentation Logging Database 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="Event Log Listener" />
          <add name="Exception Handling Database Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
</configuration>

Then during application bootstrapping load the config and register a handler for the SourceChanged event:
FileConfigurationSource configSource = new FileConfigurationSource("logging.config", true, 5000);
configSource.SourceChanged += LoggingConfigSource_SourceChanged(;
LogWriterFactory logWriterFactory = new LogWriterFactory(configSource);
Logger.SetLogWriter(logWriterFactory.Create());

And create the logic to reset the current LogWriter
        void LoggingConfigSource_SourceChanged(object sender, ConfigurationSourceChangedEventArgs e)
        {
            LogWriterFactory logWriterFactory = new LogWriterFactory(e.ConfigurationSource);
            // Dispose of the existing LogWriter and recreate
            Logger.Reset();
            Logger.SetLogWriter(logWriterFactory.Create());
        }
Note that the above code is not thread-safe so if multiple threads were logging and Reset() was called on another thread errors could result when the LogWriter is Disposed.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to