Friday, May 18, 2012
 



Search the blog


 Blog   
Jan 24

Written by: Georg Jansen
Tue, 24 Jan 2006 11:05:43 GMT

I needed to be able to change the file log4net was logging on programmatically, on the same time I wanted to use the other configuration options available in the config file.

 

After several different approaches I ended up with writing a method like the one listed below:

 

static bool ChangeLogFileName(string AppenderName, string NewFilename)

{

         

   log4net.Repository.ILoggerRepository  RootRep;

   RootRep = log4net.LogManager.GetRepository();

 

   foreach (log4net.Appender.IAppender iApp in RootRep.GetAppenders())

   {

      if (iApp.Name.CompareTo(AppenderName) == 0

                              && iApp is log4net.Appender.FileAppender)

      {

         log4net.Appender.FileAppender fApp = (log4net.Appender.FileAppender)iApp;

 

         fApp.File = NewFilename;

         fApp.ActivateOptions();

         return true;  // Appender found and name changed to NewFilename

      }

   }

 

   return false; // appender not found

}

 

The method basically search through all appenders to find the appender named in the input parameter. When an appender with the right name is found, it check if the appender is a FileAppender (or to be more precise, that the object found, implement the IFileAppender interface).

 

I anyone know a more efficient way to find a named appender, I would very much like to know about it.

 

I found out that using a standard configuration section in the config file failed if the filename was missing, and if a filename was stated that file would be created during the normal configuration (log4net.Config.XmlConfigurator.Configure()), therefore I ended up using the nul device as the filename.  My config section is listed below:

 

 

<appender name="FileAppender" type="log4net.Appender.FileAppender">

  <param name="File" value="nul"/>

  <appendToFile value="true"/>

  <layout type="log4net.Layout.PatternLayout">

    <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>

  </layout>

</appender>

 

Since the RollingFileAppender inherits the FileAppender, the same method will also work with the RollingFileAppender.

 

Using the nul device, causes the RollingFileAppender to issue some internal error messages during configuration (you will be able to see they if you turn on internal log4net debug).

 

A small sidenote on the nul device. The nul device or the “nul” filename has been around in Windows and Dos since the beginning of time, at least since Dos 2.0. I believe it is inherited from the unix operating system. If you try to write to a file named nul, the content you write to the file will simply disappear. You can try it out with Notepad, save a file on c:\nul.txt, you will be warned that the file already exists, but you will be able to save to it. But you will never be able to retrieve anything (WROM Write only memory).

 

Tags:

Re: Programmatically changing log file names in Log4net

I can't thank you enough. You just saved me a ton of time trying to figure this out on my own.

This seems easy once you see it, but if you don't understand the repository concept fully, and the docs were a little opaque to me, you might never figure this out.
____________________________
http://innerexception.blogspot.com

By Dave Murdock on   Thu, 02 Feb 2006 15:56:21 GMT

Re: Programmatically changing log file names in Log4net

Very helpful! Strange that this is not documented & more obvious.

By Rick Rigby on   Wed, 19 Jul 2006 22:14:05 GMT

Re: Programmatically changing log file names in Log4net

The approach is excellent as far as i am concerned.....
It saved a lot of digging the core of log4net

By Glitsun Cheeran on   Wed, 18 Oct 2006 11:56:19 GMT

Re: Programmatically changing log file names in Log4net

Awesome!!!! I have been looking for this for the past hour.

By Chris on   Fri, 17 Nov 2006 15:22:12 GMT

Re: Programmatically changing log file names in Log4net

You are the man! A thousand thank yous!

By Ian Watts on   Thu, 28 Dec 2006 16:14:42 GMT

Re: Programmatically changing log file names in Log4net

Just want to share my approach.

You may define a custom property for your logger (for example in the constructor of your class):
log4net.ThreadContext.Properties["LogFilePath"] = your new path;

Then you can use this property in the configuration file:











Then call
log4net.Config.XmlConfigurator.Configure();
to apply your changes

Note: Do not initialize your logger in the AssemblyInfo.cs.
[assembly: log4net.Config.XmlConfigurator()]

Here is a full snippet:

class Class1
{
private static ILog log = LogManager.GetLogger(typeof(Class1));

///


/// The main entry point for the application.
///

[STAThread]
static void Main(string[] args)

log4net.ThreadContext.Properties["LogFilePath"] = System.Configuration.ConfigurationSettings.AppSettings["NewLogFilePath"];
log4net.Config.XmlConfigurator.Configure();


log.Info("just test");
// ...
// ...

log4net.ThreadContext.Properties["LogFilePath"] = @"..\..\TestingLog\";
log4net.Config.XmlConfigurator.Configure();

log.Info("just new test");


}
}


By Max Leifer on   Mon, 13 Aug 2007 17:44:04 GMT

Re: Programmatically changing log file names in Log4net













By Max Leifer on   Mon, 13 Aug 2007 17:46:47 GMT

Re: Programmatically changing log file names in Log4net

Your appender config can be written as following:
file type="log4net.Util.PatternString" value="%property{LogFilePath}\LOG\"

By Max Leifer on   Mon, 13 Aug 2007 17:49:28 GMT

Re: Programmatically changing log file names in Log4net

really great !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

By karthic on   Fri, 17 Aug 2007 10:00:08 GMT

Re: Programmatically changing log file names in Log4net

Awesome!

By Sreenivas M on   Fri, 05 Oct 2007 18:59:06 GMT

Re: Programmatically changing log file names in Log4net

Good work budy.......:-), keep on posting...........

By Chandana on   Wed, 02 Jan 2008 15:50:16 GMT

Re: Programmatically changing log file names in Log4net

Can you please share the logic behind why we should'nt initialize the logger in AssemblyInfo.cs?

By Kumz on   Tue, 15 Apr 2008 10:28:47 GMT

Re: Programmatically changing log file names in Log4net

Excellent tip! Thanks for saving me some time!

By Jason S. on   Fri, 15 Aug 2008 20:49:15 GMT
Copyright 2005-2008 by FaktNet AS Terms Of Use Privacy Statement