Geeks With Blogs
The Life and Times of a Dev Yes, we're really that weird

Recently I had the need to change the logging level for all loggers due to the need to hit the logging statements for NCover.  This was harder than it appeared, and I couldn't really find anything out there that really documented the code you needed, so here's the code:

 

 

        private void TurnOnLogging()
        {
            
            log4net.Repository.ILoggerRepository[] repositories= log4net.LogManager.GetAllRepositories();

            //Configure all loggers to be at the debug level.
            foreach (log4net.Repository.ILoggerRepository repository in repositories)
            {
                repository.Threshold = repository.LevelMap["DEBUG"];
                log4net.Repository.Hierarchy.Hierarchy hier = (log4net.Repository.Hierarchy.Hierarchy)repository;
                log4net.Core.ILogger[] loggers=hier.GetCurrentLoggers();
                foreach (log4net.Core.ILogger logger in loggers)
                {
                    ((log4net.Repository.Hierarchy.Logger) logger).Level = hier.LevelMap["DEBUG"];
                }
            }

            //Configure the root logger.
            log4net.Repository.Hierarchy.Hierarchy h = (log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository();
            log4net.Repository.Hierarchy.Logger rootLogger = h.Root;
            rootLogger.Level = h.LevelMap["DEBUG"];

        }

Some caveat's with this:

 

1. It'll only work after you've already done something to cause the logging system to initialize.

2.  I'll only see loggers that have been created.

 

Hopefully this is helpful to someone else!

Posted on Wednesday, August 22, 2007 2:47 PM | Back to top


Comments on this post: log4net and changing the logger levels

# re: log4net and changing the logger levels
Requesting Gravatar...
Thanks. This info was very helpful!
Left by Richard H on Dec 06, 2007 4:58 AM

# re: log4net and changing the logger levels
Requesting Gravatar...
Hi,

Thanks ! Great peace of code !

I wrote some and implementation example here:
http://ysgitdiary.blogspot.com/2009/05/dynamically-set-log-level-of-log4net.html
Left by Yordan Georgiev on May 03, 2009 5:37 AM

# re: log4net and changing the logger levels
Requesting Gravatar...
Great Piece of Code and it was helpful to me too ..
Left by -M on Dec 02, 2009 7:05 AM

# re: log4net and changing the logger levels
Requesting Gravatar...
Very helpful, thanks alot!!!
Left by Marcus on Feb 04, 2010 6:31 AM

# re: log4net and changing the logger levels
Requesting Gravatar...
Nice code, exactly what I was lookging for
Windows 7
Vista
don't allow the usual rewrite of the Configuration , Configuration.Save
etc
Left by Martin Rojo on Jul 14, 2010 11:29 AM

# re: log4net and changing the logger levels
Requesting Gravatar...
hi , I would like to implement the logging level dynamically ie: when the admin want to set the logging level info then - info level should log along with debug level, if he want only debug level - only debug level should log..

How to implement this i didn't get clearly

Pls explain where to put code and how to integrate it.

thanks
Left by venkat on Feb 10, 2011 3:21 AM

# re: log4net and changing the logger levels
Requesting Gravatar...
Venkat,

First, the normal way for an admin to change the logging level is to specifically change it in the configuration section of the log4net setup. Please see the log4net documentation for examples of this. Usually, you set up the configuration to "watch" for changes in the config file. log4net does dynamic changes out of the box.

You could implement a UI to change the logging level. Basically, you'd capture the logging level they wanted (DEBUG, INFO, WARN, ERROR) and pass it as a string in place of the strings with "DEBUG" in them above. You'd want to selectively change it for only the loggers that the admin was interested in, however, so you'd need to specify the logger in the loop that turns on all logging.

I strongly recommend that you spend some time with the log4net documentation, though, since it seems that you may not have a good understanding of how log4net works.
Left by Robert May on Feb 10, 2011 7:37 PM

# re: log4net and changing the logger levels
Requesting Gravatar...
How do you make it work in a scenario where threshold is set to Error in configuration file, and it needs to be reset to debug in the code.
Left by Priya on Apr 11, 2011 1:09 PM

# re: log4net and changing the logger levels
Requesting Gravatar...
Priya,

In order to change the threshold from Error (set in config file) to Debug via code I had to do this:

ILog log = LogManager.GetLogger("Your.Logger.Here");
log.Logger.Repository.Threshold = Level.Debug;

log4net.Repository.Hierarchy.Logger logger;
logger = (log4net.Repository.Hierarchy.Logger) log.Logger;
logger.Level = Level.Debug;


HTH.

João
Left by João Toledo on Jun 09, 2011 12:51 PM

# re: log4net and changing the logger levels
Requesting Gravatar...
I want to have in web.config file any tag contains the value: low/meduim/high which I can change by my decision,

In my class I want to check the value of the tag and according the value to send message,

my questions: in which tag should I put it? where to place the tag in web.config file ?
and how can I check in my class what the tag contains?

it's very urgent for me,
thanks in advance!
TWTW
Left by twtw on Sep 13, 2011 4:12 AM

# re: log4net and changing the logger levels
Requesting Gravatar...
i loved this snippet. very very well done. thank you very much!
Left by jon on Dec 08, 2011 5:41 AM

# re: log4net and changing the logger levels
Requesting Gravatar...
Very nice piece of code!

One question: How would you update the config file with the new level once it's been set (without hand-editing it)?
Left by Jon on Mar 07, 2012 6:46 AM

# re: log4net and changing the logger levels
Requesting Gravatar...
Thanks ! very helpful..
Left by vpalmu on Dec 12, 2012 5:59 AM

Your comment:
 (will show your gravatar)


Copyright © Robert May | Powered by: GeeksWithBlogs.net