Close this search box.

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 =

  // 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.Core.ILogger[] loggers = hier.GetCurrentLoggers();
    foreach (log4net.Core.ILogger logger in loggers) {
      ((log4net.Repository.Hierarchy.Logger)logger).Level =

  // Configure the root logger.
  log4net.Repository.Hierarchy.Hierarchy h =
  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!

This article is part of the GWB Archives. Original Author: Life in my own company

Related Posts