65.9K
CodeProject is changing. Read more.
Home

Nlog Configuration

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (4 votes)

Oct 28, 2010

LGPL3
viewsIcon

26521

Nlog configuration

I’ve switched from my own logging framework to nlog. NLog seems quite flexible, is under active development and suits my needs perfectly. I’m logging both to the console and to files using the following configuration file:

  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
  </configSections>

  <nlog xmlns=" http://www.nlog-project.org/schemas/NLog.xsd"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true" 
    internalLogFile=" Nlog.txt">
    <targets>
      <target name="file" xsi:type="File"
      layout="${longdate} ${threadid:padding=3} ${level:padding=-30} 
      ${logger:padding=-30} ${message} ${exception:format=tostring}"
      fileName="${basedir}/logs/${shortdate}.txt"/>
      <target name="errors" xsi:type="File"
      layout="${longdate} ${threadid:padding=3} ${level:padding=-30} 
      ${logger:padding=-30} ${message} ${exception:format=tostring}"
      fileName="${basedir}/logs/${shortdate}.errors.log" />
      <target name="console" xsi:type="ColoredConsole"
              layout="${date:format=HH\:MM\:ss} ${threadid:padding=3} 
              ${logger:padding=-30} ${message}" />
    </targets>
    <rules>
      <logger name="*" minlevel="Trace" 
      writeTo="file, console" />
      <logger name="*" minLevel="Error" 
      writeTo="errors" />
    </rules>
  </nlog>

The console gets colored output looking like this:

10:07:41  10 AppDog.Monitor    Checking AppDog.Monitoring.MonitoredApplication

And file syntax:

2010-07-03 10:22:56.4218  10 Trace   AppDog.Monitor   Checking AppDog.Monitoring.MonitoredApplication