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 "Eric Rose (erose)" <er...@cisco.com> on 2009/01/31 04:21:32 UTC

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
 
 
 
Eric Rose
Software Engineer
Product Development

erose@cisco.com <ma...@cisco.com> 
Phone :(978) 936-1858
Cisco Systems, Inc.
500 Beaver Brook Road
Boxborough, MA 01719

www.cisco.com <http://www.cisco.com> 
 
 
 

RE: Change the log level programmatically?

Posted by "Eric Rose (erose)" <er...@cisco.com>.
Thanks to everyone for their suggestions... 
No single suggestion worked completely for me, but there were nuggets of
information that were quite helpful!
I just spent a while figuring out what works for me, and this is the
result...
 
We have a static utility class as follows, and I was able to add these
'SetLevel<Level>' methods to modify the one logging object we setup, to
change the log levels on the fly.
There is still a log4net.conf file which defines all the usual things
(log file location, appending rules, etc)
 
Here's the code:
 
using log4net;
using log4net.Appender;
using log4net.Core;
using log4net.Repository.Hierarchy;
 
    public static class LogUtility
    {
        private static readonly log4net.ILog log =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMeth
od().DeclaringType);
        private static readonly Logger logger = log.Logger as Logger;
 
        public static log4net.ILog GetLog()
        {
            return (log);
        }
 
        private static void SetLevel(Level level)
        {
            if (logger != null)
            {
                logger.Level = level;
            }
        }
        public static void SetLevelDebug()
        {
            SetLevel(Level.Debug);
        }
        public static void SetLevelInfo()
        {
            SetLevel(Level.Info);
        }
        public static void SetLevelWarn()
        {
            SetLevel(Level.Warn);
        }
        public static void SetLevelError()
        {
            SetLevel(Level.Error);
        }
        public static void SetLevelFatal()
        {
            SetLevel(Level.Fatal);
        }
        public static void SetLevelOff()
        {
            SetLevel(Level.Off);
        }
        public static void SetLevelAll()
        {
            SetLevel(Level.All);
        }
    }
 
 
Here's a code fragment that tests this:
                  LogUtility.SetLevelOff();
            LogUtility.GetLog().Debug("After SetLevelOff() debug");
            LogUtility.GetLog().Warn("After SetLevelOff() warn");
            LogUtility.GetLog().Fatal("After SetLevelOff() fatal");
 
            LogUtility.SetLevelInfo();
            LogUtility.GetLog().Debug("After SetLevelInfo() debug");
            LogUtility.GetLog().Warn("After SetLevelInfo() warn");
            LogUtility.GetLog().Fatal("After SetLevelInfo() fatal");
 
            LogUtility.SetLevelError();
            LogUtility.GetLog().Debug("After SetLevelError() debug");
            LogUtility.GetLog().Warn("After SetLevelError() warn");
            LogUtility.GetLog().Fatal("After SetLevelError() fatal");
 
            LogUtility.SetLevelWarn();
            LogUtility.GetLog().Debug("After SetLevelWarn() debug");
            LogUtility.GetLog().Warn("After SetLevelWarn() warn");
            LogUtility.GetLog().Fatal("After SetLevelWarn() fatal");
            
            LogUtility.SetLevelAll();
            LogUtility.GetLog().Debug("After SetLevelALL() debug");
            LogUtility.GetLog().Warn("After SetLevelALL() warn");
            LogUtility.GetLog().Fatal("After SetLevelALL() fatal");
 
 
And, this is what the log file output looks like:
3: erose-xp5: Feb 11 2009 15:11:07.803 -05:00: %testLog-WARN-After
SetLevelInfo() warn
4: erose-xp5: Feb 11 2009 15:11:07.803 -05:00: %testLog-FATAL-After
SetLevelInfo() fatal
5: erose-xp5: Feb 11 2009 15:11:07.803 -05:00: %testLog-FATAL-After
SetLevelError() fatal
6: erose-xp5: Feb 11 2009 15:11:07.803 -05:00: %testLog-WARN-After
SetLevelWarn() warn
7: erose-xp5: Feb 11 2009 15:11:07.803 -05:00: %testLog-FATAL-After
SetLevelWarn() fatal
8: erose-xp5: Feb 11 2009 15:11:07.803 -05:00: %testLog-DEBUG-After
SetLevelALL() debug
9: erose-xp5: Feb 11 2009 15:11:07.803 -05:00: %testLog-WARN-After
SetLevelALL() warn
10: erose-xp5: Feb 11 2009 15:11:07.803 -05:00: %testLog-FATAL-After
SetLevelALL() fatal
 
 
So, it is possible, and does work!
 
Thanks again, everyone...!
 
-esr
 
 
From: Eric Rose (erose) 
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: log4net-user@logging.apache.org
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
 
 
 
Eric Rose
Software Engineer
Product Development

erose@cisco.com <ma...@cisco.com> 
Phone :(978) 936-1858
Cisco Systems, Inc.
500 Beaver Brook Road
Boxborough, MA 01719

www.cisco.com <http://www.cisco.com> 
 
 
 

RE: Change the log level programmatically?

Posted by Francine Taylor <Fr...@genesis-fs.com>.
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:erose@cisco.com] 
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:NDURCHO@e-farmcredit.com] 
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:erose@cisco.com] 
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: log4net-user@logging.apache.org
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:image001.gif@01C7EFF5.57209E90

	
Eric Rose
Software Engineer
Product Development

erose@cisco.com <ma...@cisco.com> 
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

Re: Change the log level programmatically?

Posted by Ron Grabowski <ro...@yahoo.com>.
// untested
public class HierarchyLoggerLevelScope : IDisposable
{
    private readonly Level originalLevel;
    private readonly Logger logger;

    public HierarchyLoggerLevelScope(ILog log, Level level)
    {
        logger = log.Logger as Logger;
        
        if (logger != null)
        {
            originalLevel = logger.Level;
            logger.Level = level;
        }
    }

    public void Dispose()
    {
        if (logger != null)
        {
            logger.Level = originalLevel;
        }
    }
}




----- Original Message ----
From: Eric Rose (erose) <er...@cisco.com>
To: Log4NET User <lo...@logging.apache.org>
Cc: Eric Rose (erose) <er...@cisco.com>
Sent: Wednesday, February 11, 2009 10:11:12 AM
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:NDURCHO@e-farmcredit.com] 
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:erose@cisco.com] 
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: log4net-user@logging.apache.org
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:image001.gif@01C7EFF5.57209E90

    
Eric Rose
Software Engineer
Product Development

erose@cisco.com <ma...@cisco.com> 
Phone :(978) 936-1858

    Cisco Systems, Inc.
500 Beaver Brook Road
Boxborough, MA 01719

www.cisco.com <http://www.cisco.com> 

RE: Change the log level programmatically?

Posted by "Eric Rose (erose)" <er...@cisco.com>.
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:NDURCHO@e-farmcredit.com] 
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:erose@cisco.com] 
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: log4net-user@logging.apache.org
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:image001.gif@01C7EFF5.57209E90

	
Eric Rose
Software Engineer
Product Development

erose@cisco.com <ma...@cisco.com> 
Phone :(978) 936-1858

	Cisco Systems, Inc.
500 Beaver Brook Road
Boxborough, MA 01719

www.cisco.com <http://www.cisco.com> 

	 

	

 

 


RE: Change the log level programmatically?

Posted by Nick Durcholz <ND...@e-farmcredit.com>.
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:erose@cisco.com] 
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: log4net-user@logging.apache.org
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.GetCurrentMethod().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:image001.gif@01C7EFF5.57209E90

	
Eric Rose
Software Engineer
Product Development

erose@cisco.com <ma...@cisco.com> 
Phone :(978) 936-1858

	Cisco Systems, Inc.
500 Beaver Brook Road
Boxborough, MA 01719

www.cisco.com <http://www.cisco.com> 

	 

	

 

 


RE: Change the log level programmatically?

Posted by "Eric Rose (erose)" <er...@cisco.com>.
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: log4net-user@logging.apache.org
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
 
 
 
Eric Rose
Software Engineer
Product Development

erose@cisco.com <ma...@cisco.com> 
Phone :(978) 936-1858
Cisco Systems, Inc.
500 Beaver Brook Road
Boxborough, MA 01719

www.cisco.com <http://www.cisco.com>