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 Nicko Cadell <ni...@neoworks.com> on 2005/08/14 22:34:18 UTC

RE: How to pass parameters through coding in log4net Applications

The parameters configured through the config file are just public
properties on the objects. If you want to programmatically create
appenders you can do so as follows:


log4net.Repository.Hierarchy.Hierarchy hierarchy =
(log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository
();

log4net.Appender.FileAppender appender = new
log4net.Appender.FileAppender();
appender.File = "foo.txt";

log4net.Layout.PatternLayout patternLayout = new
log4net.Layout.PatternLayout("%message%newline");
patternLayout.ActivateOptions();
appender.Layout = patternLayout;
appender.ActivateOptions();
hierarchy.Root.AddAppender(appender);


If what you want to do is to programmatically update an appender that
you have specified in a config file then the first thing you need to do
is to locate the appender object.

You could do something like this to change the File property of a
FileAppender:

foreach(log4net.Appender.IAppender appender in
log4net.LogManager.GetRepository().GetAppenders())
{
  if (appender is log4net.Appender.FileAppender)
  {
    log4net.Appender.FileAppender fileAppender =
(log4net.Appender.FileAppender)appender;

    // Change value of properties
    fileAppender.File = "other.txt";

    // Reset file appender properties
    fileAppender.ActivateOptions();
  }
}

Cheers,
Nicko

> -----Original Message-----
> From: news [mailto:news@sea.gmane.org] On Behalf Of vicky
> Sent: 28 July 2005 07:47
> To: log4net-user@logging.apache.org
> Subject: How to pass parameters through coding in log4net Applications
> 
> hi,
> 
> Please tell me how can we pass the parameters like log 
> filename for a log4net file Appender through Code.
> Same i have problem in case of ADONetAppender like Connection String.
> 
> Thanks & regards
> Vicky
> 
> 
>