The configuration file below shows the use of three logging targets - console, rolling file and the event viewer. It sets a name of the application and keeps all the logs written to an app specific location - whether a folder with that name or an event viewer source name.
More adaptations can be done using the built in parameters listed below this example
To see how to setup NLog with .NET core please refer to: https://alexrait.blogspot.com/2022/07/configure-nlog-with-net-core-6.html
To see how to configure syslog with NLog please refer to: https://alexrait.blogspot.com/2021/08/how-to-send-syslog-messages-with.html
<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">
<variable name="appname" value="MyAppName"/>
<variable name="brief" value="${longdate} | ${level} | ${logger} | ${message} | ${exception:format=ToString}"/>
<variable name="verbose" value="${longdate} | ${machinename} | ${level} | ${logger} | [${callsite}] ${message} | ${exception:format=ToString}"/>
<targets>
<target name="fileTarget"
xsi:type="File"
fileName="..\logs\${appname}\log.txt"
archiveFileName="..\logs\${appname}\log.{#}.txt"
archiveEvery="Day"
archiveNumbering="Rolling"
layout="${verbose}"
maxArchiveFiles="7" />
<target name="eventLogTarget" xsi:type="EventLog" layout="${brief}" log="Application" source="${appname}"/> <target name="consoleTarget" xsi:type="ColoredConsole" layout="${brief}" />
<target xsi:type="Null" name="blackhole" />
</targets>
<rules>
<logger name="Microsoft.*" minlevel="Error" writeTo="fileTarget" final="true" />
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
<logger name="*" minlevel="Debug" writeTo="fileTarget" />
<logger name="*" minlevel="Debug" writeTo="eventLogTarget" />
<logger name="*" minlevel="Debug" writeTo="consoleTarget" />
</rules>
</nlog>
</configuration>
Additional prameters:
${callsite} - Writes the calling method.
${callsite-linenumber} - Writes the line number in code
${windows-identity} - Identity
${aspnet-request-ip} - Client IP
${aspnet-request-headers} - Request headers
${aspnet-request-method} - Request method
${aspnet-request-posted-body} - Post payload
To filter log add the filter element inside the logger:
<logger name="*" minlevel="Debug" writeTo="consoleTarget" >
<filters defaultAction="Log">
<when condition="contains('${message}', 'Some text')" action="Ignore"/>
</filters>
</logger>
For additional options to filter read here: https://github.com/NLog/NLog/wiki/When-Filter