Changing log file path at run time

Topics: Logging Application Block
May 18, 2009 at 7:05 PM

What would be the best way to change the path of the log file at run time?

 

May 18, 2009 at 7:18 PM

  you can change the filepath at runtime

 

           Configuration config = ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
           LoggingSettings loggingSettings = (LoggingSettings)config.GetSection(LoggingSettings.SectionName);
           FlatFileTraceListenerData flatfiletracedata = loggingSettings.TraceListeners.Get("FlatFile TraceListener") as FlatFileTraceListenerData;
           flatfiletracedata .Filename = {your new filepath}

once you start logging to  a file ....the file will be locked as the log file until application restart

 

May 18, 2009 at 8:04 PM
Edited May 18, 2009 at 8:17 PM

Thanks for you reply. Is there something else I need to do? That code does in fact change the FileName property but the logs are still being written to same location.

 

May 18, 2009 at 8:09 PM

once you have made your changes in the constructor you need to add

config.Save();

this will write the new file path into your actual config file.

 

May 18, 2009 at 8:17 PM

Ah.. thanks. That was what I forgot.