Enterprise Library 6.0 Logging disable/enable dynamically

Topics: Logging Application Block
Mar 7 at 3:51 AM
I have requirement to log the message in Log file and Event Viewer. But I want if I call one method then it should log the message in Log file not in Event Viewer at that time. And, if I call second method then it should log the message in Event Viewer not in Log file at the same time.

Please advise if there is way to do this using Enterprise Library 6.0.
Mar 7 at 6:54 AM
Probably the easiest way to do what you want would be drive the conditional logging with categories. Since you seem to know the conditions ahead of time (during development and not at runtime) you can define 3 categories:
  1. One that logs to the Event Log
  2. One that logs to a File
  3. One that logs to both the Event Log and the file in #2
The config would look something like this:
<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="EventLog">
        <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" />
            <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                fileName="trace.log" formatter="" />
        </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" />
        </formatters>
        <categorySources>
            <add switchValue="All" name="EventLog">
                <listeners>
                    <add name="Event Log Listener" />
                </listeners>
            </add>
            <add switchValue="All" name="File">
                <listeners>
                    <add name="Flat File Trace Listener" />
                </listeners>
            </add>
            <add switchValue="All" name="EventLogAndFile">
                <listeners>
                    <add name="Event Log Listener" />
                    <add name="Flat File 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" />
                </listeners>
            </errors>
        </specialSources>
    </loggingConfiguration>
</configuration>

Then in code you could do something like:
logWriter.Write("Only to the event log", "EventLog");
logWriter.Write("Only to the file", "File");
logWriter.Write("Write to both", "EventLogAndFile");
Marked as answer by vikramgera on 3/8/2016 at 1:16 AM
Mar 7 at 10:11 AM
Thank you so much. It worked. :)

One more thing if you could tell me -
I would like to give name to Log file on date basis. For ex for today's file name should be - 20160307_exception.log. What is the easiest way to do it.
I'm doing with below code. is there any other way you can tell me?
            string path = @"C:\misc\";
            string filename = path + DateTime.Now.ToString("yyyyMMdd") + "_Exception.txt";

            Configuration xmlConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            LoggingSettings setting = (LoggingSettings)xmlConfiguration.GetSection(LoggingSettings.SectionName);
            RollingFlatFileTraceListenerData flatFile = setting.TraceListeners.Get(1) as RollingFlatFileTraceListenerData;
            flatFile.FileName = filename;
            xmlConfiguration.Save();
            ConfigurationManager.RefreshSection(LoggingSettings.SectionName);
            IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
            LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
            Logger.SetLogWriter(logWriterFactory.Create(), false);

            Logger.Write(messagebody, "File");
Mar 7 at 2:36 PM
Unfortunately, there really isn't a great way of doing this. Another option would be programmatic configuration. Also, every day you need to re-configure the block in order to change the date of the log file.