You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Patrick Shirley <Pa...@soeidental.com> on 2010/08/06 04:28:02 UTC

Help needed to programmatically set the appender

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


RE: Help needed to programmatically set the appender

Posted by Roy Chastain <Ro...@roychastain.org>.
You need to look at ActivateOptions.  It is a method on the appender
class.  I know it affects threshold .  I do not know what else.

 

Below is code I wrote years ago to enable and disable logging in a end
user application in which the end user can stop and start the logging
multiple times in a session.  It is not great code, but it mostly works
in a narrow definition of the word works.

 

For what it is worth.  I have wrestled with the "write the config to a
file issue".  I convinced myself that it was not so bad.  I have many
windows services that use log4Net and I want the administrator to be
able to control the amount of tracing/logging and the location of the
file and the number of files etc.  I wrote an administration program
that gathers this info and creates the log4Net config file from skeletal
file with %keyword% in it.  (I change the %keyword% based on admin
input.)  I then ship this file to the service via WCF, the service
overwrites its own config file which log4Net was monitoring.  Not
beautiful, but works well and much easier than figuring out all the
internals of log4Net.

 

 

    public void ConfigureTrace (TraceLevelType level, string path)

    {

     log4net.Repository.Hierarchy.Hierarchy    hierarchy;

 

      hierarchy =
(log4net.Repository.Hierarchy.Hierarchy)(log4net.LogManager.GetRepositor
y());

      if (level != TraceLevelType.TLT_Off)

      {

        if (had_traced_before)

          rfa.AppendToFile = true;

        else

          if (path != null && path != "")

          rfa.File = path;

        rfa.ActivateOptions();

        hierarchy.Root.AddAppender(rfa);

        hierarchy.Configured = true;

        if (level == TraceLevelType.TLT_Normal)

          rfa.Threshold = log4net.Core.Level.Trace;

        else

          rfa.Threshold = log4net.Core.Level.Verbose;

        LogTrace.Trace(this,"Transport {0} tracing initialized at
{1}",ai.ProductVersion,

          DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));

        had_traced_before = true;

      }

      else

      {

        if (rfa.Threshold != log4net.Core.Level.Off)

          LogTrace.Trace(this,"Transport {0} tracing terminated at
{1}",ai.ProductVersion,DateTime.

            Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));

        rfa.Threshold = log4net.Core.Level.Off;

      }

    }

 

----------------------------------------------------------------------

Roy Chastain

 

 

 

From: Patrick Shirley [mailto:Patrick.Shirley@soeidental.com] 
Sent: Thursday, August 05, 2010 22:28
To: log4net-user@logging.apache.org
Subject: Help needed to programmatically set the appender

 

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