1
votes

Do you know if there is a way to programmatically enable certain logging level?

I want to keep only error logging enabled during normal application usage but be able to turn on more detailed when needed, by passing a command line argument to the application.

1

1 Answers

3
votes

Given a basic logger configured like this

<targets>
  <target xsi:type="File" name="payroll" fileName="c:\temp\payroll.log"             
        layout="${longdate} ${uppercase:${level}} ${message}" />
</targets>
<rules>
    <logger name="*" minlevel="Error" writeTo="payroll" />
</rules>

You can programatically add to the the log levels

    static void Main(string[] args)
    {
        if (args.Contains("Info"))
        {
            var logger = NLog.LogManager.Configuration.LoggingRules.First(x => x.Targets.First().Name == "payroll");
            logger.EnableLoggingForLevel(LogLevel.Info);    
        }

        var proc = new PayrollProcessor();
        proc.Process();
    }