Changing the log file location at runtime with log4net

I’ve been using log4net in a recent project, and recently needed to modify things so that the log file can be changed at runtime.  It’s being used with a console application, and the log filename needs to change depending on what the command line parameters are.

There’s a great entry about setting the log file location at runtime with a DOM configured log4net, which explains how to do this.  In my case, I’ve used a rolling file appender, and implemented things a little differently, though the concept is the same.

I’ve put together a basic console application showing how this works, which you can download.

It’s a simple console application, where log4net is configured from an external configuration file.  The main things to look at are the file and datePattern settings.  Using the standard RollingFileAppender, this will creates logfiles in the Logs directory, named Log-yyyy-mm-dd.log.

The custom RollingFileAppender class adds a static property called prefix.  When set, the log filename will change to Log-Prefix-yyyy-mm-dd.log.  To set the prefix, you simply need to set the Prefix property, before initialising the log object.

RollingFileAppender.Prefix = "MyPrefix";
ILog logger = LogManager.GetLogger(typeof (MyClass));
logger.Debug("A debug message");

Overall, I’m really impressed with log4net.  It’s a solid logging framework, and makes it much easier to add logging to applications.  It’s simple to implement, and a lot more convenient than rolling your own logging system.

[tags]logging, log4net[/tags]

This entry was posted in Uncategorized by Mun. Bookmark the permalink.

3 thoughts on “Changing the log file location at runtime with log4net

  1. Hi
    This was very helpful changing the filename once. However, I have a single assembly that creates 3 services. I get the name to change once when I create the logger (in the service OnStart method). However, I cannot get it to change again once the first service has started – all the logging goes in the first name chosen no matter which service is started first. All the services use the same OnStart method.
    I checked that my App.Config entries for the appender were basically the same as yours except for the path. I also tried a base.ActivateOptions after setting the base.File in the set method for the File property.
    Any help would be appreciated.

  2. Yep, you’re right. It looks like log4net caches the logger, with no clear way to change the log file target once the logger has been created (or not easily anyway).

    One thing you might want to consider is specifying multilple appenders in your log4net config file (one for each service), and using a filter on each one to only log relevant messages.

    For an example of a log4net config file using filters, check out Tom Graham’s blog entry, a realistic log4net config file.

    Haven’t tried it myself (yet), though it looks like it might solve your problem.

  3. This worked for me:

    public static void UpdateFileAppenderPaths(string theNewPath) {
    log4net.Repository.Hierarchy.Hierarchy h =
    (log4net.Repository.Hierarchy.Hierarchy) log4net.LogManager.GetRepository();
    foreach (IAppender a in h.Root.Appenders) {

    if (a is FileAppender) {
    FileAppender fa = (FileAppender) a;
    fa.File = theNewPath;


Leave a Reply

Your email address will not be published. Required fields are marked *