Show/Hide Toolbars

The possibly simplest custom logging configuration is a configuration that disables any logging. While we do not recommend to uses such a configuration it might be useful for debugging purposes.

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true">
  <targets>
      <target
        name="NullTarget"
        xsi:type="Null"
        />
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="NullTarget" />
  </rules>
</nlog>

Listing 1: Disabling all logging by defining a null target.

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
      <target
        name="WindowsEventLogInfo"
        xsi:type="EventLog"
        layout="REDBEX ${longdate} ${message}"
        source="RedbexApplicationServer"
        machineName="."
        eventId="1"/>
    <target name="ColoredConsole" xsi:type="ColoredConsole"/>
  </targets>
  <rules>
    <logger name="*" minlevel="Trace" writeTo="WindowsEventLogInfo">

      <filters>
          <when condition="length('${message}') > 100" action="Ignore" />
          <when condition="equals('${logger}','MyApps.SomeClass')" action="Ignore" />
          <when condition="(level >= LogLevel.Debug and contains('${message}','PleaseDontLogThis')) or level==LogLevel.Warn" action="Ignore" />
          <when condition="not starts-with('${message}','PleaseLogThis')" action="Ignore" />
        </filters>
    </logger>
  </rules>
</nlog>

Listing 2: A custom logging configuration that uses advanced filtering in the rules section.

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
      <target name="logfile" xsi:type="File" fileName="c:\temp\logfile.txt" layout="${longdate} ${level:uppercase=true} ${message} ${onexception:${newline}EXCEPTION OCCURRED${newline}${exception:format=tostring}}"/>
  </targets>
  <rules>
      <logger name="*" minlevel="Info" writeTo="logfile" />
  </rules>
</nlog>

Listing 3: A very simple logging configuration, just writing to a single file.

© 2021 AFRY Austria GmbH, www.redbex.com