Tutorial :%property does not work with RollingFileAppender in log4net



Question:

I have been doing what it was suggested in Best way to dynamically set an appender file path 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.


Solution:1

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


Solution:2

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).


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »