How about something like this?
private void ChangeLogLevel(string pLoggerName, Level pLogLevel) {
Logger ll = SafeSearchForLogger(pLoggerName);
if (ll != null) {
ll.Level = pLogLevel;
}
}
// this method looks for a named logger. If it doesn't already exist,
null
// is returned
public static Logger SafeSearchForLogger(string loggerName) {
foreach (ILogger a in ThisHierarchy().GetCurrentLoggers()) {
if (a.Name.Equals(loggerName)) {
return (Logger)a;
}
}
return null;
}
-----Original Message-----
From: Eric Rose (erose) [mailto:[email protected]]
Sent: Wednesday, February 11, 2009 7:11 AM
To: Log4NET User
Cc: Eric Rose (erose)
Subject: RE: Change the log level programmatically?
Thanks Nick,
I'm not going to hack the log4net code, so that's not really an option.
I'm asking about this, because one of the developers had asked if there
was a way to do this.
But, it sounds like it's not really possible, unless we want to modify
the config file...
So, thanks for the information!
-esr
-----Original Message-----
From: Nick Durcholz [mailto:[email protected]]
Sent: Wednesday, February 11, 2009 10:01 AM
To: Log4NET User
Subject: RE: Change the log level programmatically?
There isn't a clean way to do this with the interfaces provided. If you
really MUST have this, you could probably hack something up, but it
would require breaking some of the abstractions that the framework tries
to enforce. You could also try loading the config xml into memory,
modifying it using the dom, and then reconfiguring the entire heirarchy
using log4net.Config.XmlConfigurator.Configure(ILoggerRepository
repository, XmlElement element). I've never tried this, so can't really
say what pitfalls and 'gotchas' come along with doing that.
Why do you need to change the level of a logger at runtime in the first
place? There may be a better way to accomplish the task at hand without
doing that.
________________________________
From: Eric Rose (erose) [mailto:[email protected]]
Sent: Wednesday, February 11, 2009 9:37 AM
To: Log4NET User
Subject: RE: Change the log level programmatically?
Hi,
I sent this question a couple of weeks ago, but haven't heard any
feedback from anyone...
Could someone offer some suggestions here, or let me know that this
can't be done?!
Thanks,
-esr
From: Eric Rose (erose)
Sent: Friday, January 30, 2009 10:22 PM
To: [email protected]
Subject: Change the log level programmatically?
Hopefully this is an easy question which has already been asked (and
answered) before.
I searched online, but could not find a reasonable answer...
Is there a way to change the default log level of a logger via C#
program control?
I don't need the setting to persist, so I don't want to have to change
the XML config file and re-read it.
In my C# code, I have a ILog variable that we use for all logging,
defined as follows:
private static readonly log4net.ILog log =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMeth
od().DeclaringType);
I'd like to be able to somehow be able to use that 'log' variable, and
set it's log level (INFO, WARN, ERROR, OFF, ALL, etc) to whatever I want
in the code.
Any help or pointers to useful examples would be appreciated!
Thanks,
-esr
cid:[email protected]
Eric Rose
Software Engineer
Product Development
[email protected] <mailto:[email protected]>
Phone :(978) 936-1858
Cisco Systems, Inc.
500 Beaver Brook Road
Boxborough, MA 01719
www.cisco.com <http://www.cisco.com>
==========================================
NOTICE: The contents of this e-mail message and any attachments are intended
solely for the addressee(s) named in this message. This communication is
intended to be and to remain confidential. If you are not the intended
recipient of this message, or if this message has been addressed to you in
error, please immediately alert the sender by reply e-mail and then delete this
message and its attachments. Do not deliver, distribute or copy this message
and/or any attachments and if you are not the intended recipient, do not
disclose the contents or take any action in reliance upon the information
contained in this communication or any attachments.
Thank you