29

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?

Pang
  • 9,564
  • 146
  • 81
  • 122
Ramon Smits
  • 2,482
  • 1
  • 18
  • 20

1 Answers1

37

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" />
Pang
  • 9,564
  • 146
  • 81
  • 122
Ramon Smits
  • 2,482
  • 1
  • 18
  • 20
  • 3
    This finally worked for me when I slowed down reading it long enough to notice that it was maxlevel and not minlevel and that the idea is like you are catching and swallowing those ones by omitting writeTo. Also, the example you cite no longer appears on the page linked to. – Milton Nov 19 '22 at 21:58