The Life and Times of a Dev

Yes, we're really that weird
posts - 179, comments - 302, trackbacks - 106

My Links

News

Twitter









Tag Cloud

Archives

Post Categories

Play

Work

log4net and changing the logger levels

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!

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Wednesday, August 22, 2007 2:47 PM |

Feedback

Gravatar

# re: log4net and changing the logger levels

Thanks. This info was very helpful!
12/6/2007 4:58 AM | Richard H
Gravatar

# re: log4net and changing the logger levels

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
5/3/2009 5:37 AM | Yordan Georgiev
Gravatar

# re: log4net and changing the logger levels

Great Piece of Code and it was helpful to me too ..
12/2/2009 7:05 AM | -M
Gravatar

# re: log4net and changing the logger levels

Very helpful, thanks alot!!!
2/4/2010 6:31 AM | Marcus
Gravatar

# re: log4net and changing the logger levels

Nice code, exactly what I was lookging for
Windows 7
Vista
don't allow the usual rewrite of the Configuration , Configuration.Save
etc
7/14/2010 11:29 AM | Martin Rojo
Gravatar

# re: log4net and changing the logger levels

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
2/10/2011 3:21 AM | venkat
Gravatar

# re: log4net and changing the logger levels

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.
2/10/2011 7:37 PM | Robert May
Gravatar

# re: log4net and changing the logger levels

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.
4/11/2011 1:09 PM | Priya
Gravatar

# re: log4net and changing the logger levels

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
6/9/2011 12:51 PM | João Toledo
Gravatar

# re: log4net and changing the logger levels

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
9/13/2011 4:12 AM | twtw
Gravatar

# re: log4net and changing the logger levels

i loved this snippet. very very well done. thank you very much!
12/8/2011 5:41 AM | jon
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: