Is it possible to dynamically set log folder and file name in C# which overrides the config value(file name) in the Listeners section of logging configuration? Tried to follow the below link but it is applicable only for EL 5 Log messages going to previously created log file
This is how i Initialize EL Logging:
IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
Logger.SetLogWriter(logWriterFactory.Create(), false);
ExceptionPolicyFactory factory = new ExceptionPolicyFactory(configurationSource);
ExceptionPolicy.SetExceptionManager(factory.CreateManager());
These are my web.config entries inside the listeners tag:
<add name="RollingFlatFileTraceLog" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="C:\logs\Trace.log" footer=""
formatter="CustomRollingFlatFileLogFormatter"
header="" rollFileExistsBehavior="Overwrite" rollInterval="Day"
rollSizeKB="2048" maxArchivedFiles="52" />
<add name="RollingFlatFileErrorLog" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="C:\logs\Error.log" footer="----------------------------------------"
formatter=" CustomRollingFlatFileLogFormatter "
header="----------------------------------------" rollFileExistsBehavior="Overwrite"
rollInterval="Day" rollSizeKB="2048" maxArchivedFiles="52" />
I use Logger.Write and ExceptionPolicy.HandleException to write to the corresponding trace and error logs.
The best (and easiest!) approach would be to use programmatic configuration to set the filename.
However, you can use a similar approach to modify settings at runtime. Setup the configuration in an external file and use a file configuration source in app/web.config. An external file is necessary to avoid a configuration is read-only exception.
And configure the external file (entlib.config in this case):
Use the
SerializableConfigurationSource
from the blog posting:Bootstrap the blocks at startup: