How to configure NLog to only log from a certain level for a logger namespace for *all* targets

Ramon Smits picture Ramon Smits · Apr 12, 2016 · Viewed 19.5k times · Source

I have the following loggers configured.

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target name="file" xsi:type="File" fileName="trace.log"/>
    <target name="trace" xsi:type="OutputDebugString"/>
    <target name="console" xsi:type="ColoredConsole" />
  </targets>
  <rules>
      <logger name="*" minlevel="Info" writeTo="file" />
      <logger name="*" minlevel="Info" writeTo="trace" />
      <logger name="*" minlevel="Info" writeTo="console" />
  </rules>
</nlog>

I want everything for Component.* to only be logged from WARN and higher for all loggers. With NHibernate, this is easy:

<logger name="NHibernate.SQL">
  <level value="OFF"/>
</logger>

I tried to add the following:

<logger name="Component.*" minlevel="Warn" final="true" />

This doesn't work.

How do I only log from a certain level for a logger namespace for all targets?

Answer

Ramon Smits picture Ramon Smits · Apr 14, 2016

The solution is:

<logger name="Component.*" maxlevel="Info" final="true" />

You basically say, for logger(s) X, I want to skip all log entries that match Info or lower as this does not have the writeTo attribute.

It is documented here:

https://github.com/nlog/NLog/wiki/Configuration-file

With the sample:

<logger name="Name.Space.*" minlevel="Debug" maxlevel="Error" final="true" />