logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Patrick Shirley <Patrick.Shir...@soeidental.com>
Subject Help needed to programmatically set the appender
Date Fri, 06 Aug 2010 02:28:02 GMT
Hi,

I'm having trouble figuring out how to programmatically set the appender for a logger at run
time. I want my logger to be configured by an XML file to obtain default values but then to
dynamically change the appender.

I default my logger to this:

_log = LogManager.GetLogger("SoeiDental.Logging");//MethodBase.GetCurrentMethod().DeclaringType
            XmlConfigurator.Configure();


Where _log is a public static log4net.ILog. Whenever I log, I'm logging using _log.Warn()
or _log.Fata().

My first problem is that I cannot see anyway to access or modify the appender used by the
logger named "SoeiDental.Logging". I've read through the Configuration chapter of the manual
and studied the SDK. Unfortunately the Configuration chapter seemed very focused on configuration
using XML file and doesn't cover programmatic configuration of a specific logger instance.
Secondly if I resign myself to setting the root logger in this way:

            log4net.Appender.FileAppender appender = new log4net.Appender.FileAppender();
            appender.File = path;
            appender.AppendToFile = true;
            appender.Name = "NewLogFileAppender";
            appender.Layout = new log4net.Layout.PatternLayout("%date [%thread] %-5level %message%newline");
            BasicConfigurator.Configure(appender);

It doesn't seem to have any effect. My logging still uses the appender originally specified
in the XML file. The way I understand it "SoeiDental.Logging" is a child of the root logger
and so whenever I log using this it ought to use all the appenders of its ancestors.

At this stage the only thing I can see to do is to have my code write to the .config to modify
the "File" parameter but this quite twisted way of doing things, I shouldn't have to write
out to file in order to what ought to be possible with code.

Any help would be greatly appreciated.
Thanks


Mime
View raw message