Friday, May 24, 2013
 
The best way to analyze your logs! Minimize
 Log4Net Mail archive   

The Log4Net mailing list is a great source of information about using log4Net, in this forum we collect all the messages in the log4net user list and some selected threads from the developer list.

Subject: Adding appenders programmatically
Prev Next
You are not authorized to post a reply.

Author Messages
Vladimir Kovalenko

02/23/2006 9:32 AM  

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

 

 

Nicko Cadell

02/23/2006 6:31 PM  

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

You are not authorized to post a reply.
Forums > Log4Net > Log4Net Mail archive > Adding appenders programmatically



ActiveForums 3.7

 

 

 

 

 

 

 

 

Log4Net Dashboard

Log analysis and monitoring made easy!

Log4Net Dashboard is a log viewer that can read log statements from a variety of logging output targets.

You can download a free developer version.

  

Check it out!

On the demonstration site you can try it  with live data.demo.l4ndash.com - Try Log4Net Dashboard with live data

The mail archive is a copy of all the mail sent to the mail address: log4net-user@logging.apache.org, organized as a forum.

If you would like to participate in the mail list, send a mail to log4net-user-subscribe@logging.apache.org.

More information about the mailing list is available on: http://logging.apache.org/log4net/support.html

 

A complete topic list is available and can be viewed here (warning, it takes some time to load)

 

Copyright 2005-2008 by FaktNet AS Terms Of Use Privacy Statement