Change log4net logging level programmatically

Solution 1:

None of these solutions present here worked for me. It wasn't changing at runtime

Here is what worked for me:

((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root.Level = Level.Debug;
((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).RaiseConfigurationChanged(EventArgs.Empty);

You have to call RaiseConfigurationChanged after making changes to its config.

Solution 2:

Finally found a working solution, here.

The big pieces were:

  • need to set the threshold on all loggers, including the "cannot be retrieved by name" root logger
  • need to get the Level from the Hierarchy's LevelMap

Big thanks to Eddie for asking good pointed questions, which led me to google the right words. I never would have figured this out alone.

(Aside: Repository, Hierarchy, Logger, RootLogger, LevelMap -- I had no idea it was even possible to make a logging library this complex. It's got about 20 layers of indirection, which I'm sure makes it flexible enough for anything, but makes it nearly impossible to do simple things like "don't log any messages above threshold X". Gah!)

Solution 3:

There's a simple way to do it:

LogManager.GetRepository().Threshold = Level.Info;

More information can be found here.

Solution 4:

If this works for someone, here is how I implemented in my web application a toggle level endpoint exposed via an API call, so I can change the logging level on the fly.

public HttpResponseMessage Post([FromBody] string logLevel)
{
    var newLevel = ToggleLogging(logLevel);

    // Create your own response here.
    return newLevel;
}

And the actual implementationt

private Level ToggleLogging(string logLevel)
{

    Level level = null;

    if (logLevel != null)
    {
        level = LogManager.GetRepository().LevelMap[logLevel];
    }


    if (level == null)
    {
        level = Level.Warn;
    }

    ILoggerRepository[] repositories = LogManager.GetAllRepositories();

    //Configure all loggers to be at the same level.
    foreach (ILoggerRepository repository in repositories)
    {
        repository.Threshold = level;
        Hierarchy hier = (Hierarchy)repository;
        ILogger[] loggers = hier.GetCurrentLoggers();
        foreach (ILogger logger in loggers)
        {
            ((Logger)logger).Level = level;
        }
    }

    //Configure the root logger.
    Hierarchy h = (Hierarchy)LogManager.GetRepository();
    Logger rootLogger = h.Root;
    rootLogger.Level = level;

    return level;
}

Solution 5:

JeanT's answer doesnt seem to work with the latest version of log4net.

I adapted it to get it working, this changes to Debug level, but alter as required.

foreach(var r in LogManager.GetAllRepositories()) { ((log4net.Repository.Hierarchy.Hierarchy)r).Root.Level = Level.Debug; ((log4net.Repository.Hierarchy.Hierarchy)r).RaiseConfigurationChanged(EventArgs.Empty); }