Home > DeveloperSection > Forums > Most useful NLog configurations
ezra heywood
ezra heywood

Total Post:145

Posted on    April-17-2013 1:55 AM


 1 Reply(s)
 3943  View(s)
Rate this:

Hi Everyone!

What are the best or most useful configurations for logging with NLog? (These can be simple or complex, as long as they're useful.)

I'm thinking of examples like automatically rolling over log files at a certain size, changing the layout (log message) whether or not there is an exception, escalating the log level once an error has occurred, etc.

Here are some links:

Thanks in advance!


Total Post:604

Posted on    April-17-2013 9:09 AM

Hi Ezra!

Treating exceptions differently

We often want to get more information when there is an exception. The following configuration has two targets, a file and the console, which filter on whether or not 

there is any exception info. (EDIT: Jarek has posted about a new method of doing this in vNext.)

The key is to have a wrapper target with xsi:type="FilteringWrapper" condition="length('${exception}')>0"

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.mono2.xsd"
      internalLogFile="nlog log.log"
    <variable name="VerboseLayout" value="${longdate} ${level:upperCase=true} ${message} (${callsite:includSourcePath=true})" />
    <variable name="ExceptionVerboseLayout" value="${VerboseLayout} (${stacktrace:topFrames=10}) ${exception:format=ToString}" />

    <targets async="true">
        <target name="file" xsi:type="File" fileName="log.log"

        <target name="fileAsException" xsi:type="FilteringWrapper" condition="length('${exception}')>0">
            <target xsi:type="File" fileName="log.log" layout="${ExceptionVerboseLayout}" />

        <target xsi:type="ColoredConsole" name="console"

        <target xsi:type="FilteringWrapper" condition="length('${exception}')>0" name="consoleException">
            <target xsi:type="ColoredConsole"
                    layout="${ExceptionVerboseLayout}" />

        <logger name="*" minlevel="Trace" writeTo="console,consoleException" />
        <logger name="*" minlevel="Warn" writeTo="file,fileAsException" />


Don't want to miss updates? Please click the below button!

Follow MindStick