EntLib5.0 - issue not being able to override filename read from web.config (loggingConfiguration....)

Topics: Logging Application Block
May 31, 2013 at 11:13 PM
I followed your discussion on the link http://entlib.codeplex.com/discussions/394890

and accordingly tried to do as Randy suggested. I was able to build without any issues. But when it came to executing this line of code


it fails saying that it is readonly and gives the exception. So none of the solutions resolved my problem of not being to modify the filename for the log.

I am using EntLib5.0. I would appreciate what is the workaround for this, and I don't want to and also the company policy doesn't let us write/modify the config file and this

being ASP.Net application no changes can be done while the application is running and using the web.config. Thanks in advance and please respond at the earliest.

And here is my code to create the new listener in memory

private static void OverrideListenerAndWriteLog(object message)
        string fileName = "";
        string sFormatter = "";
        RollInterval rollInterval = 0;
        RollFileExistsBehavior rollFileExists = 0;

        IConfigurationSource configSource = ConfigurationSourceFactory.Create();
        var loggingSettings = configSource.GetSection(LoggingSettings.SectionName) as LoggingSettings;

        //Get current tracelistener settings
        var builder = new ConfigurationSourceBuilder();
        var currTraceListenerData = loggingSettings.TraceListeners.Get("Rolling Flat File Trace Listener");
        var flatFileTraceListenerData = currTraceListenerData as RollingFlatFileTraceListenerData;
        fileName = flatFileTraceListenerData.FileName;
        fileName = RootAndFormatFileName(fileName);
        sFormatter = ((RollingFlatFileTraceListenerData)currTraceListenerData).Formatter;
        rollInterval = ((RollingFlatFileTraceListenerData)currTraceListenerData).RollInterval;
        rollFileExists = ((RollingFlatFileTraceListenerData) currTraceListenerData).RollFileExistsBehavior;

        RollingFlatFileTraceListenerData rollingListenerData = new RollingFlatFileTraceListenerData();
        rollingListenerData.FileName = fileName;
        rollingListenerData.Formatter = sFormatter; //"Text Formatter";
        rollingListenerData.MaxArchivedFiles = 10;
        rollingListenerData.RollFileExistsBehavior = rollFileExists; //RollFileExistsBehavior.Overwrite;
        rollingListenerData.RollInterval = rollInterval; // RollInterval.Day;
        rollingListenerData.RollSizeKB = 10;
        rollingListenerData.TimeStampPattern = "yyyyMMddHHmm";
        rollingListenerData.Name = "blah";

        var traceListenerData = rollingListenerData as TraceListenerData;
        //************* Note: Begin - is where the failure occurs ****************
        //************* Note:  End - is where the failure occurs ****************

        IUnityContainer container = new UnityContainer();

        UnityContainerConfigurator configurator = new UnityContainerConfigurator(container);

        var loggingXmlConfigSource = new SerializableConfigurationSource();
        loggingXmlConfigSource.Add(LoggingSettings.SectionName, loggingSettings);

        EnterpriseLibraryContainer.ConfigureContainer(configurator, loggingXmlConfigSource);
        IServiceLocator locator = new UnityServiceLocator(container);
        EnterpriseLibraryContainer.Current = locator;
Any help is greatly appreciated.

NOTE: I used this discussion link http://entlib.codeplex.com/discussions/394890
as my basis and thought it would work as per Randy Levy (Enterprise Library support engineer). But alas it didn't.
Jun 1, 2013 at 12:50 PM
Edited Jun 1, 2013 at 12:51 PM
It's hard to see in the thread but if you look at this post which "hijacked" the original thread you will see that the specific scenario being addressed in that thread has the Enterprise Library Logging configuration in an external config file (file configuration source pointing to MYDOMAIN.Common.config). When configuration is in the web.config file you will receive the read only message but with an external config file it works so that is a caveat I added later on for other posters.

Randy Levy
Enterprise Library support engineer
Support How-to