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: Log4net
Date Sun, 14 Aug 2005 22:39:11 GMT
There are 2 ways to control the level at which events are allowed to be
logged.

Firstly the log event is routed to an appender based on the logger it is
logged against. The logger has a level which determines if the event is
routed to the appender or not. You can change the level for a logger
programmatically like this:

((log4net.Repository.Hierarchy.Logger)log4net.LogManager.GetLogger("my.l
ogger.name").Logger).Level = log4net.Core.Level.Error;

Once the event has been routed to the appender it is up to the appender
itself to decide if it should write the event or not. Appenders have a
threshold level that is used to ignore events that have a level below
the threshold value. You can change the threshold value for an appender
by looking up the appender and setting the property:

foreach(log4net.Appender.IAppender appender in
log4net.LogManager.GetRepository().GetAppenders())
{
  // Look for the appender you want to change.
  // You can check the appender Name if that helpd
  if (appender is log4net.Appender.FileAppender)
  {
    log4net.Appender.FileAppender fileAppender =
(log4net.Appender.FileAppender)appender;

    // Change value of properties
    fileAppender.Threshold = log4net.Core.Level.Error;
  }
}

If you want to create an appender you can do so like this:

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 you remember the appender instance you can just change the threshold
on it, there is no need to find it each time using the GetAppenders()
method.

Cheers,
Nicko

PS. Definitely a log4net-user list question

> -----Original Message-----
> From: Vince Lasorsa [mailto:lavinci@gmail.com] 
> Sent: 08 August 2005 15:02
> To: Nicko Cadell
> Subject: Log4net
> 
> Nicko,
> I have a simple question, which I can not find an answer to 
> in the log4net documentation.
> 
> Q: How can I enable/disable a Level, Programmatically from 
> within a C# program. I have several reasons why I do NOT want 
> to use an XML config file. I'm trying to programatically 
> control this in C#.
> 
> example: I want to create a fileAppender and turn on and off 
> various log.level such as:
> level.info = false;
> level.error=true;
> level.fatal=false;
> etc...
> 
> 
> THANK you in advance for you time and consideration!
> 
> --
> Vince Lasorsa
> 
> 

Mime
View raw message