Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set isdebugenabled in NLog

Tags:

asp.net

nlog

I try to find a way to set isdebugenabled false or true in configuration file,

so I can turn it on or off whenever I need. Thanks !

like image 440
maxisam Avatar asked Aug 07 '12 19:08

maxisam


People also ask

What is NLog config in C#?

NLog is a . Net Library that enables you to add high-quality logs for your application. NLog can also be downloaded using Nugget in Visual Studio. Targets are used to display, store, or pass log messages to another destination.

Does NLog work with .NET core?

nuget.org/packages/NLog/… NLog 4.7 has been released! See news post: nlog-project.org/2020/03/28/nlo… While older versions of NLog works well on ASP.NET Core 3, we have optimized the NLog.


1 Answers

Using a really simple logging setup

<targets>
  <target xsi:type="File"
          name="debug"
          fileName="c:\temp\debug.txt"
          layout="${longdate} ${uppercase:${level}} ${message}" 
  />
</targets>

<rules>
  <logger name="*" minlevel="Trace" writeTo="debug" />
</rules>

And an equally simple code sample

class Program
{
    static void Main(string[] args)
    {
        var logger = new NLog.LogFactory().GetCurrentClassLogger();

        if (logger.IsDebugEnabled)
        {
            logger.Debug("this is a debug message");
        }

        logger.Debug("this is another debug message");
    }
}

When minLevel is set to Debug or Trace, both logger.Debug statements will write to the log. If you raise minLevel to a higher level (Info, Warn, Off) neither statement will be written to the log. logger.Debug checks IsDebugEnabled (which is inferred from the log level).

You can certainly get a performance increase (in cases where you are going to be logging calculated values and not just strings) by checking IsDebugEnabled, and changing minLevel for the logger is the way to toggle this.

like image 161
Joe Avatar answered Sep 21 '22 19:09

Joe