tags:

views:

32

answers:

2

Hi, I have been doing what it was suggested in http://stackoverflow.com/questions/571876/best-way-to-dynamically-set-an-appender-file-path/3371566#3371566 to set the file name dynamically, using %property{}, but the file is created as "(nul).log" below is the code

log4net.GlobalContext.Properties["service"] = _servicename.ToString();
_flatFileLogger = LogManager.GetLogger("FlatFileLogger");
_flatFileLogger.Debug(logData.ToString());

I have declared in the XML file as

file type="log4net.Util.PatternString" value="C:\My_Log\%property{service}.log"

Help much appreciated.

A: 

Make sure you're setting the property before you initialize log4net.

Patrick Steele
thanks for reply patrick,I am declaring FlatFileLogger at the start of the program and using it as showed,just after the %property is set1. log4net.GlobalContext.Properties["service"] = _servicename.ToString(); 2. _flatFileLogger = LogManager.GetLogger("FlatFileLogger");3. _flatFileLogger.Debug(logData.ToString());the same sequence
raaga_sai
But where are you initializing the log4net framework? Your call to "log4net.Config.XmlConfigurator.Configure()" must be done **after** setting the GlobalContext property but **before** you first access the logger.
Patrick Steele
Thank you Patrick... Using log4net.Config.XmlConfigurator.Configure() has actually solved the issue..
raaga_sai
A: 

Do not confuse loggers and appenders. Most people use one logger per class: This way your log messages can easily be put in relation to your classes.

Appenders on the other hand have nothing to do with the way your program is structured. In your configuration you can tell which loggers should use which appenders. This gives you great flexiblity, that you would not have you if you use "appenders" directly in code.

Do you have any other logger that write a statement before the one you posted? If so then I would assume that the file appender is already initialized (with null in the name).

Stefan Egli