logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nicko Cadell" <ni...@neoworks.com>
Subject RE: Adding appenders programmatically
Date Thu, 23 Feb 2006 17:31:10 GMT
Yes it is possible to programmatically configure log4net. There are a
number of different ways of doing it.

1) Store your custom config in a file or stream and call one of the
log4net.Config.XmlConfigurator.Configure() methods to load the config
file.

2) Create your custom config as an in memory XML document and call the
log4net.Config.XmlConfigurator.Configure(XmlElement) method to load the
configuration.

3) Call the log4net APIs directly to set the levels and add appenders.
It helps to have some util functions:

// Set the level for a named logger
public static void SetLevel(string loggerName, string levelName)
{
  log4net.ILog log = log4net.LogManager.GetLogger(loggerName);
  log4net.Repository.Hierarchy.Logger l =
(log4net.Repository.Hierarchy.Logger)log.Logger;

  l.Level = l.Hierarchy.LevelMap[levelName];
}

// Add an appender to a logger
public static void AddAppender(string loggerName,
log4net.Appender.IAppender appender)
{
  log4net.ILog log = log4net.LogManager.GetLogger(loggerName);
  log4net.Repository.Hierarchy.Logger l =
(log4net.Repository.Hierarchy.Logger)log.Logger;

  l.AddAppender(appender);
}

// Find a named appender already attached to a logger
public static log4net.Appender.IAppender FindAppender(string
appenderName)
{
  foreach (log4net.Appender.IAppender appender in
log4net.LogManager.GetRepository().GetAppenders())
  {
    if (appender.Name == appenderName)
    {
      return appender;
    }
  }
  return null;
}

In order to set the level for a logger and add an appender reference you
can then use the following calls:

SetLevel("Log4net.MainForm", "ALL");
AddAppender("Log4net.MainForm", FindAppender("AdoNetAppender"));

Note that the FindAppender will only find an existing appender which is
already attached to a logger. If an appender is specified in the config
file but not actually referenced by any loggers then it will be ignored.
If this is the case you will need to programmatically create the
appender as well, for example:

// Create a new file appender
public static log4net.Appender.IAppender CreateFileAppender(string name,
string fileName)
{
  log4net.Appender.FileAppender appender = new
log4net.Appender.FileAppender();
  appender.Name = name;
  appender.File = fileName;
  appender.AppendToFile = true;
  
  log4net.Layout.PatternLayout layout = new
log4net.Layout.PatternLayout();
  layout.ConversionPattern = "%d [%t] %-5p %c [%x] - %m%n";
  layout.ActivateOptions();

  appender.Layout = layout;
  appender.ActivateOptions();

  return appender;
}


You can then associate it with the logger as follows:

AddAppender("Log4net.MainForm", CreateFileAppender("FileAppender",
"C:\\foo.log"));


Cheers,
Nicko


> -----Original Message-----
> From: Vladimir Kovalenko [mailto:gaset.kovalenko@gmail.com] 
> Sent: 23 February 2006 08:33
> To: log4net-user@logging.apache.org
> Subject: Adding appenders programmatically
> 
> I need to add programmatically references to appenders to a 
> specific logger. In other words I want to configure the 
> following logger from code. Is it possible?
> 
>  
> 
> <logger name="Log4net.MainForm">
> 
>       <level value="ALL" />
> 
>       <appender-ref ref="AdoNetAppender" />
> 
>       <appender-ref ref="SmtpAppender" />
> 
>       <appender-ref ref="CustomAppender" />
> 
> </logger>
> 
>  
> 
> Best regards,
> 
> Vladimir Kovalenko
> 
>  
> 
>  
> 
> 

Mime
View raw message