The simplest way to dynamically change the log4net configuration is to install the xml configurator to view changes to the configuration file:
[assembly: XmlConfigurator(Watch = true)]
When you make some changes to the application configuration file (the one that lives next to exe), log4net will automatically reconfigure itself, and further output will go to another file.
If for some reason you want to do this programmatically, you need to get an instance of the file application from the log repository (aka Hierarchy):
var fileAppender = LogManager.GetLoggerRepository() .GetAppenders() .OfType<FileAppender>() .FirstOrDefault(fa => fa.Name == "LogFileAppender");
And change the settings:
if (fileAppender != null) { fileAppender.File = Path.Combine(Environment.CurrentDirectory, "foo.txt"); fileAppender.ActivateOptions(); }
Remember that this simple File property does not open a new file - it simply sets the name of the file that should be opened during appender activation. So, you need to activate it manually right after changing the file path.
source share