Quick overview about how and why to use NLog

I needed to use a Loggin Framework, every time using loggin frameworks, it ends up with wasting some hours, because the configuration that makes me feel it’s better to create my own :-), I already started to create a small one but I found this is exactly re-enventing the wheel so I decide to give some times for different Loggin frameworks’s configurations.

Log4Net, I used that one because I found it has been used for some projects we have, it took all the day for fixing up some configurations errors that I found at the end they are bugs, I needed to make some workarounds to fix them. I don’t recommended anyone to use this framework as it not updated from long time ago (2006).

NLog, it was very simple, very nice …I decide to summarize my overview about it:

Here are the steps to use NLog:

  • Add reference to NLog framework using NUget (PM>Install-Package NLog)
  • Add config file using NUget (PM> Install-Package NLog.Config)
  • Add at least one target in the Target section in the config file
  • Add at least one rule in the Rules section that use any target from the Target section
  • Create an instance of the Logger class inside the class you want to use NLog

    [sourcecode language=”csharp”] private static readonly Logger Logger = LogManager.GetCurrentClassLogger() [/sourcecode]

  • Call any method of Logger object (Log, Trace, Debug, etc..)


  • NLog tutorial
  • All Target types
  • All Layouts Renders
  • we can make combinations for target and rules for e.x,
    • We can use one target for each rule—> Error in Event Log, Info in a File
    • We can use multiple rules for the same target —> Error and Info in the same File
    • We can use multiple targets for the same rule —> Error and Info in both Event Log and in the same File or even in many Files


Here is the config file with some notes and description of some elements and attributes

[code language=”xml”]

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"

See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
<target name="logfile" xsi:type="File" fileName="file.txt" />
<target name="console" xsi:type="Console" />

<target name="errors" xsi:type="File" fileName="ErrorLog.txt"
layout="Caller: ${callsite:className=true:fileName=true:includeSourcePath=true
:methodName=true} ${newline} LogDirectory: ${nlogdir}
${newline} Process: ${processname} ${newline} ${longdate}
${message} ${exception:format=tostring} "/>

<!– add your targets here –>

<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />

name >>>>>
Name of the target to be used in the Rules section

xsi:type >>>>>
There are many types e.x, File, EventLog, Database
Mail, Messagebox and many others
The list of all types: https://github.com/NLog/NLog/wiki/Targets

fileName >>>>>
If the type is a file then we need to provide a file name,
The the default is the place where the program executed but
we can put any path and it support NUC (Network) too \machinesharedFolder

layout >>>>>
How and what data we want to include in the logging message,
It has many place holders that start with "$" each placeholder has
itsown attributes E.x: ${processname}, ${newline}, {message}, we
can include string between the placeholders e.x: $(processname} this
is string ${message},the list of
all layouts: https://github.com/NLog/NLog/wiki/Layout-renderers




<logger name="*" minlevel="Trace" writeTo="logfile" />
<logger name="*" minlevel="Trace" writeTo="errors" />
<logger name="*" minlevel="Info" writeTo="console" />

<!– add your logging rules here –>

<logger name="*" minlevel="Trace" writeTo="f" />

name >>>>>
Name of the Class that has the Logger object,
* means all classes

minlevel >>>>>
Trace, Debug, Info, Warn, Error, Fatal
If we put the Trace this will show all of them
If we put Info, this will show (Info, Warn, Error, Fatal)

writeTo >>>>>
Target name, pick it from the Target section



Share This: