Discussion:
[Log4net-users] Programmatically setting the logger level
Kevin Wittmer
2004-01-16 23:55:57 UTC
Permalink
Hello,

I am hoping that someone can rattle off a couple of lines of code that would
show me how to programmatically set the logger level.

If I want to accomplish the following:

<appender>
<evaluator type="log4net.spi.LevelEvaluator">
<param name="Threshold" value="WARN"/>
</evaluator>
</appender>

In C# using the various log4net interface and classes, etc. How can this be
done?

My application currently includes the following:

log4net.Appender.RollingFileAppender fileAppender =
new log4net.Appender.RollingFileAppender(
new log4net.Layout.PatternLayout(
"%d [%t] %-5p %c - %m%n"), log4netFile);
fileAppender.MaxFileSize = 65536;
fileAppender.MaxSizeRollBackups = 3;
log4net.Config.BasicConfigurator.Configure(fileAppender);

Thanks! Maybe we could use the reply(s) from this posting to update the
log4net documentation to include such an example. Or, maybe the solution to
this could make its way into the log4net FAQ.

Kevin

_________________________________________________________________
Get a FREE online virus check for your PC here, from McAfee.
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
Nicko Cadell
2004-01-19 14:32:20 UTC
Permalink
Kevin,

Only the BufferingAppenderSkeleton classes support the Evaluator (i.e. the
ADONetAppender, BufferingForwardingAppender, RemotingAppender, SMTPAppender
and SmtpPickupDirAppender appenders).

If what you want is to set the event threshold for the appender then you do
not need to use the Evaluator at all. i.e. the following configuration would
work:

<appender>
<param name="Threshold" value="WARN" />
</appender>

Or the equivalent:

<appender>
<threshold value="WARN" />
</appender>

To set this in code you would use:

fileAppender.Threshold =
log4net.LogManager.GetLoggerRepository().LevelMap["WARN"];

Hope this helps,

Nicko
-----Original Message-----
Sent: 16 January 2004 23:56
Subject: [Log4net-users] Programmatically setting the logger level
Hello,
I am hoping that someone can rattle off a couple of lines of
code that would
show me how to programmatically set the logger level.
<appender>
<evaluator type="log4net.spi.LevelEvaluator">
<param name="Threshold" value="WARN"/>
</evaluator>
</appender>
In C# using the various log4net interface and classes, etc.
How can this be
done?
log4net.Appender.RollingFileAppender fileAppender =
new log4net.Appender.RollingFileAppender(
new log4net.Layout.PatternLayout(
"%d [%t] %-5p %c - %m%n"), log4netFile);
fileAppender.MaxFileSize = 65536;
fileAppender.MaxSizeRollBackups = 3;
log4net.Config.BasicConfigurator.Configure(fileAppender);
Thanks! Maybe we could use the reply(s) from this posting to
update the
log4net documentation to include such an example. Or, maybe
the solution to
this could make its way into the log4net FAQ.
Kevin
_________________________________________________________________
Get a FREE online virus check for your PC here, from McAfee.
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim,
CA. http://www.eclipsecon.org/osdn
_______________________________________________
Log4net-users mailing list
https://lists.sourceforge.net/lists/listinfo/log4net-users
Nicko Cadell
2004-01-19 23:35:40 UTC
Permalink
Greg,
Post by Kevin Wittmer
log4net.Appender.RollingFileAppender fileAppender =
new log4net.Appender.RollingFileAppender(
new log4net.Layout.PatternLayout(
"%d [%t] %-5p %c - %m%n"), log4netFile);
fileAppender.MaxFileSize = 65536;
fileAppender.MaxSizeRollBackups = 3;
log4net.Config.BasicConfigurator.Configure(fileAppender);
the fileAppender is created and then used to configure log4net (the call to
BasicConfigurator.Configure), therefore this appender is attached to the
logger. Because this is using the BasicConfigurator rather than the
DOMConfigurator no config files are used.

If you want to switch logging on and off for the whole of log4net then you
should set the threshold on the logger repository rather than on the
appenders. This is checked sooner so will improve performance and is easier
to do programmatically. The logger repository is the object that manages the
loggers.

To disable all logging set the threshold level for the repository to OFF:

LogManager.GetLoggerRepository().Threshold =
LogManager.GetLoggerRepository().LevelMap["OFF"];

To enable logging set the threshold to ALL (this is the default value):

LogManager.GetLoggerRepository().Threshold =
LogManager.GetLoggerRepository().LevelMap["ALL"];



Nicko
Post by Kevin Wittmer
-----Original Message-----
From: Ismay, Greg (CALBRIS)
Sent: 19 January 2004 23:02
To: Nicko Cadell
Subject: RE: [Log4net-users] Programmatically setting the logger level
nicko,
im pretty keen on getting logging to switch on
programatically without having to write to the config file
and i think this is what this thread was about..
fileAppender.Threshold =
log4net.LogManager.GetLoggerRepository().LevelMap["WARN"];
i take it your fileAppender is an instance of the
log4net.appender.fileAppender class as the fileAppender class
doesnt seem to have any shared methods.
if it is an instance, then what ties this back to the
instance that the loggers are using, as setting the threshold
on an instance ive just instantiated doesnt (and i guess
shouldnt) do anything!
any ideas?
thanks
greg
-----Original Message-----
Sent: Tuesday, 20 January 2004 12:32 AM
Subject: RE: [Log4net-users] Programmatically setting the logger level
Kevin,
Only the BufferingAppenderSkeleton classes support the
Evaluator (i.e. the ADONetAppender,
BufferingForwardingAppender, RemotingAppender, SMTPAppender
and SmtpPickupDirAppender appenders).
If what you want is to set the event threshold for the
appender then you do not need to use the Evaluator at all.
i.e. the following configuration would
<appender>
<param name="Threshold" value="WARN" />
</appender>
<appender>
<threshold value="WARN" />
</appender>
fileAppender.Threshold =
log4net.LogManager.GetLoggerRepository().LevelMap["WARN"];
Hope this helps,
Nicko
-----Original Message-----
Sent: 16 January 2004 23:56
Subject: [Log4net-users] Programmatically setting the logger level
Hello,
I am hoping that someone can rattle off a couple of lines of
code that would
show me how to programmatically set the logger level.
<appender>
<evaluator type="log4net.spi.LevelEvaluator">
<param name="Threshold" value="WARN"/>
</evaluator>
</appender>
In C# using the various log4net interface and classes, etc.
How can this be
done?
log4net.Appender.RollingFileAppender fileAppender =
new log4net.Appender.RollingFileAppender(
new log4net.Layout.PatternLayout(
"%d [%t] %-5p %c - %m%n"), log4netFile);
fileAppender.MaxFileSize = 65536;
fileAppender.MaxSizeRollBackups = 3;
log4net.Config.BasicConfigurator.Configure(fileAppender);
Thanks! Maybe we could use the reply(s) from this posting to
update the
log4net documentation to include such an example. Or, maybe
the solution to
this could make its way into the log4net FAQ.
Kevin
_________________________________________________________________
Get a FREE online virus check for your PC here, from McAfee.
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim,
CA. http://www.eclipsecon.org/osdn
_______________________________________________
Log4net-users mailing list
https://lists.sourceforge.net/lists/listinfo/log4net-users
-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn
_______________________________________________
Log4net-users mailing list
https://lists.sourceforge.net/lists/listinfo/log4net-users
Kevin Wittmer
2004-01-20 14:03:39 UTC
Permalink
[Setting the logger level programmatically]

Thank you for the reply. Unfortunately, the following code fragment is not
possible with version 1.1.1.

fileAppender.Threshold =
log4net.LogManager.GetLoggerRepository().LevelMap["WARN"];

The method GetLoggerRepository is not available in this implementation.
LoggerWarning is implemented as a field/property, but I see no LevelMap
Hashtable. There is a RenderedMap field/property, but I don't believe that
is the same thing.

[* Next release of log4net? *]

It's possible that the 1.1.1 implementation is now quite dated. That begs
the question: Is there a planned release of log4net any time soon? How could
readers of this list help in moving up the release date? Is there additional
testing that we could divided up among ourselves to validate a final release
build?

Kevin,
Post by Kevin Wittmer
Subject: RE: [Log4net-users] Programmatically setting the logger level
Date: Mon, 19 Jan 2004 14:32:20 -0000
Kevin,
Only the BufferingAppenderSkeleton classes support the Evaluator (i.e. the
ADONetAppender, BufferingForwardingAppender, RemotingAppender, SMTPAppender
and SmtpPickupDirAppender appenders).
If what you want is to set the event threshold for the appender then you do
not need to use the Evaluator at all. i.e. the following configuration
would
<appender>
<param name="Threshold" value="WARN" />
</appender>
<appender>
<threshold value="WARN" />
</appender>
fileAppender.Threshold =
log4net.LogManager.GetLoggerRepository().LevelMap["WARN"];
Hope this helps,
Nicko
-----Original Message-----
Sent: 16 January 2004 23:56
Subject: [Log4net-users] Programmatically setting the logger level
Hello,
I am hoping that someone can rattle off a couple of lines of
code that would
show me how to programmatically set the logger level.
<appender>
<evaluator type="log4net.spi.LevelEvaluator">
<param name="Threshold" value="WARN"/>
</evaluator>
</appender>
In C# using the various log4net interface and classes, etc.
How can this be
done?
log4net.Appender.RollingFileAppender fileAppender =
new log4net.Appender.RollingFileAppender(
new log4net.Layout.PatternLayout(
"%d [%t] %-5p %c - %m%n"), log4netFile);
fileAppender.MaxFileSize = 65536;
fileAppender.MaxSizeRollBackups = 3;
log4net.Config.BasicConfigurator.Configure(fileAppender);
Thanks! Maybe we could use the reply(s) from this posting to
update the
log4net documentation to include such an example. Or, maybe
the solution to
this could make its way into the log4net FAQ.
Kevin
_________________________________________________________________
Get a FREE online virus check for your PC here, from McAfee.
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim,
CA. http://www.eclipsecon.org/osdn
_______________________________________________
Log4net-users mailing list
https://lists.sourceforge.net/lists/listinfo/log4net-users
_________________________________________________________________
Find high-speed ‘net deals — comparison-shop your local providers here.
https://broadband.msn.com

Loading...