Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically reconfigure Log4Net

I'm looking for tips on the best way to reconfigure the Log4Net logging level dynamically in my ASP.NET apps. I generally use a simple configuration where the root logger defines the default logging level, e.g.

<log4net>
    <root>
    <level value="INFO" />
    <appender-ref ref="..." />
    <appender-ref ref="..." />
    ... etc ...     
    </root>
    ... etc

and there may be several appenders, each with filters to define the logging levels they use.

  1. The first thing I'd like to be able to do would be to allow Administrators to connect to an admin page that enables them to (a) view the current level for the root logger and (b) dynamically change it. I don't want to use "ConfigureAndWatch" and write to the configuration file on disk because I don't want these changes to persist when the application is recycled.

  2. Next I'd like to go further, and on an Admin page be able to display a TreeView with all current Loggers that exist in the application, and their current logging level. And allow the administrator to be able to change the logging level selectively at any level of the hierarchy.

The idea is to to create a generic admin page that I can put into all my apps that allows administrators to selectively enable DEBUG-level logging dynamically for troubleshooting purposes.

I find the Log4Net APIs a bit confusing, can anyone point to samples or show the best way to achieve this.

Update:

Both answers are equally good so I've accepted the first - thanks. To reprise, I can get all current loggers as follows:

foreach (log4net.ILog log in log4net.LogManager.GetCurrentLoggers())
{
    log4net.Repository.Hierarchy.Logger logger = 
         (log4net.Repository.Hierarchy.Logger)log.Logger;
    Debug.WriteLine(
        String.Format("{0} Parent {1} Level {2} EffectiveLevel {3}<br>",
        logger.Name,
        logger.Parent.Name,
        logger.Level == null ? "<null>" : logger.Level.Name,
        logger.EffectiveLevel
        )
        );
}
  • EffectiveLevel is the effective level - same as Level if the latter is not null, otherwise inherited from the parent.

  • At least one of the loggers returned above will have the root logger as parent, which enables me to get a reference to the root logger.

With the above it should be possible to reconstruct the logger hierarchy.

Update 2

Thanks again. I've implemented an ASP.NET server control that displays the logger hierarchy in a TreeView with checkboxes, and allows the user to dynamically change the logging level at any node in the hierarchy. Works great and I'll be putting it on Admin page in all my ASP.NET Web and Web Service apps!

like image 663
Joe Avatar asked Apr 10 '09 16:04

Joe


2 Answers

Are you looking for something like this (untested code):

foreach (ILog logger in log4net.LogManager.GetCurrentLoggers())
{
  ((log4net.Repository.Hierarchy.Logger)logger).Level = 
      log4net.Core.Level.Error;
}

You could obviously pull out the logger name, etc. in the same manner.

like image 108
dommer Avatar answered Oct 12 '22 12:10

dommer


I have successfully programmatically changed the Logging level of a log4net logger, but it's not obvious how to do so from the public API. Given this Logger:

private readonly log4net.ILog mylogger;

You have to do the following fancy footwork to set it to Debug:

((log4net.Repository.Hierarchy.Logger)mylogger.Logger).Level =
 log4net.Core.Level.Debug;

For some situations -- I don't know what causes this more complicated requirement -- you may need to follow the extra steps shown in the article log4net and changing the logger levels.

like image 32
Eddie Avatar answered Oct 12 '22 12:10

Eddie