Reconfigure NLog LoggingConfiguration filters programmatically

I know I’m a year late but I just ran into similar scenario where I needed to provide dynamic control of the logger to users. Fortunately, this is relatively easy with nlog. Here I’m just enabling Trace level logging to an already created Logger but obviously you could do anything you wanted including adding new Targets/Rules or just completely replace the LoggerConfiguration with your programmatically generated one.

Basic ColoredConsole nlog.config:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target xsi:type="ColoredConsole" name="c"
            layout="${longdate} ${uppercase:${level}} ${message}" />
  </targets>
  <rules>
    <logger name="*" minlevel="Error" writeTo="c" />
  </rules>
</nlog>

Simple console application:

public class Program 
{
  //Initialize default Logger from config file
  private static readonly Logger m_log = LogManager.GetCurrentClassLogger();

  public static Logger Log 
  {
      get { return m_log; }
  }

  public static void Main(string[] args) 
  {
    Log.Trace("You won't see me because config is at LogLevel.Error");
    EnabledTraceForAllRules();
    Log.Trace("You will see me now though!");

    //Pause console window
    Console.WriteLine("Press any key to continue...");
    Console.ReadKey(true);

    /* Prints:
    2013-05-07 16:04:22.7050 TRACE You will see me now though!
    Press any key to continue...
    */
  }

  public static void EnabledTraceForAllRules() 
  {
    foreach(var rule in LogManager.Configuration.LoggingRules)
    {
      rule.EnableLoggingForLevel(LogLevel.Trace);
    }

    //Call to update existing Loggers created with GetLogger() or 
    //GetCurrentClassLogger()
    LogManager.ReconfigExistingLoggers();
  }
}

If you need to alter the Logging configuration from outside the process and it has to be done programmatically, I would suggest going with WCF. You could expose a small WCF service from either your application or website that provides methods for reconfiguring nlog.

Leave a Comment