Click here to Skip to main content
15,892,927 members
Articles / Programming Languages / XML

Trace Activities with NLog

Rate me:
Please Sign up or sign in to vote.
4.60/5 (5 votes)
5 Oct 2013CPOL4 min read 45.4K   4   23  
Today’s post is dedicated to NLog – one of the logging libraries available for .NET developers.
<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
  </configSections>
  
  <system.diagnostics>
    <trace autoflush="true" />
    <sources>
      <source name="TestSource" switchValue="All">
        <listeners>
          <add name="nlog" />
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="nlog" type="LowLevelDesign.NLog.NLogTraceListener, LowLevelDesign.NLog.Ext" />
    </sharedListeners>
  </system.diagnostics>

  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        internalLogToConsole="true" internalLogLevel="Info">
    <extensions>
      <add prefix="lld" assembly="LowLevelDesign.NLog.Ext" />
    </extensions>
    <targets>
      <target name="file" xsi:type="File" fileName="c:\logs\test.log" deleteOldFileOnStartup="true" layout="${longdate}|${event-context:item=RelatedActivityID}-&gt;${lld.activityid}|${processid}(${threadid})|${logger}|${uppercase:${level}}|${message}${onexception:|Exception occurred\:${exception:format=tostring}}" />
      <target name="console" xsi:type="ColoredConsole" layout="${longdate}|${lld.activityid}-&gt;${event-context:item=RelatedActivityID}|${processid}(${threadid})|${logger}|${uppercase:${level}}|${message}${onexception:|Exception occurred\:${exception:format=tostring}}" />
    </targets>
    <rules>
      <logger name="TestLogger" minlevel="Debug" writeTo="console" />
      <logger name="TestLogger" minlevel="Debug" writeTo="file" />
      <logger name="TestSource" minlevel="Debug" writeTo="console" />
      <logger name="TestSource" minlevel="Debug" writeTo="file" />
    </rules>
  </nlog>
</configuration>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Poland Poland
Interested in tracing, debugging and performance tuning of the .NET applications.

My twitter: @lowleveldesign
My website: http://www.lowleveldesign.org

Comments and Discussions