Dynamically change FlatFileTraceListener

Topics: Enterprise Library Core, Logging Application Block
Mar 9, 2010 at 4:34 PM

I am attempting to dynamically change the file the FlatFileTraceListener logs to at runtime.  The code snippet below allows me to do this, but it affects other instances of the same app that may be running.  I need to be able to modify the filename per instance.



public static void SetEnterpriseLibaryFlatFileTraceListener(string filename)
            string filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Format("{0}.config", Process.GetCurrentProcess().MainModule.FileName));
            ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
            configFileMap.ExeConfigFilename = filepath;
            System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

            if (!config.HasFile)
                throw new ApplicationException(string.Format("Unable to locate configuration file at location {0}.", config.FilePath));

            LoggingSettings loggingSection = (LoggingSettings)config.GetSection(LoggingSettings.SectionName);

            FlatFileTraceListenerData flatFileTraceListener = (FlatFileTraceListenerData)loggingSection.TraceListeners.Get("FlatFile TraceListener");

            if (flatFileTraceListener == null)
                throw new ApplicationException(string.Format("Unable to locate 'FlatFile TraceListener' in the configuration section {0}", LoggingSettings.SectionName));

            if(filename.IndexOfAny(Path.GetInvalidFileNameChars()) != -1)
                throw new ArgumentException("Invalid filename.  Contains illegal characters");

            flatFileTraceListener.FileName = filename;

Mar 9, 2010 at 9:49 PM


have you tried not actually running config.Save()?  I think that method persist changes to the disk which then effects other running instances of the app.  

Mar 9, 2010 at 10:03 PM

I tried not calling the config.Save() method.  Without the save method call, the default trace.log filename is used.  Good idea though.


Mar 10, 2010 at 6:40 AM

Yes, you should use it to persist the changes. Here are some related threads:

  1. http://entlib.codeplex.com/Thread/View.aspx?ThreadId=83457
  2. http://entlib.codeplex.com/Thread/View.aspx?ThreadId=82444
  3. http://entlib.codeplex.com/Thread/View.aspx?ThreadId=76280

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.

Mar 10, 2010 at 2:52 PM

Thanks for the posts, but none of them really address the issue.  I need to be able to modify the filename the application logs to and be able to restrict it to the application's domain.  Any time I make a change currently, the change affects all instances of the app that are running.  I want each app instance to be able to control the filename is logs to.


Mar 11, 2010 at 9:07 AM


Since that the config file is shared. A change to that will affect all instance of your app. One workaround that I can think of is to use the logging application block programmatically : http://entlib.codeplex.com/Thread/View.aspx?ThreadId=63664 .

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.

Mar 12, 2010 at 3:28 PM

Thanks.  I think that will do the trick!