Nlog configuration

I needed to add Nlog tracing that must be configurable from the config file. Finding a simple sample took a me a while. Therefor I’m putting the needed pieces here.

App.config

<?xml version=“1.0” encoding=“utf-8” ?>
<configuration>
  <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”
    autoReload=“true”
    throwExceptions=“false”>

    <variable name=“appName” value=“sample” />
    <targets async=“true”>
      <target xsi:type=“File”
                name=“default”
                fileName=“sample.log”
                keepFileOpen=“false”
                DeleteOldFileOnStartup=“true”
            />

    </targets>
    <rules>
      <logger name=“*” writeTo=“default” minlevel=“Info” />
    </rules>
  </nlog>
</configuration>

And then in code, you can just do the following.

using NLog;
        Logger logger = LogManager.GetLogger(“mylogger”);
        logger.Log(LogLevel.Debug, “Hellow world”);

Leave a Reply