NLog - disable a specific logger
Asked Answered
A

2

6

I'd like to set all loggers to log to a file except one for a specific class. I can't seem to figure out how to do it. Currently I'm trying the following nlog.config

No matter what I do, the interceptor is logging

<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="${basedir}/logs/log.log" 
                maxArchiveFiles="1" archiveAboveSize="1000000" />
    </targets>
    <rules>
        <logger name="*" minlevel="Debug" writeTo="file" />
        <logger name="MyApp.DbInterceptionConfig+LoggingEFInterceptor" minlevel="Info" writeTo="file" />

    </rules>
</nlog>
Alarcon answered 22/6, 2015 at 4:14 Comment(1)
George, line 10 appears to be extraneous or missing the begging of the line.Telmatelo
D
7

Adding final="true" to the class will tell it to stop running thru the rest of the rules when that one is hit. Because of this, the order in which you define the rules may be important as well in case you wanted more then one rule to hit but not others.

Dialyser answered 22/6, 2015 at 14:57 Comment(0)
K
6

As of today, you can use maxlevel=Off to disable a logger.

<logger name="Name.Space.Class3" maxlevel="Off" final="true" /> <!-- Blackhole that stops everything -->
<logger name="Name.Space.*" writeTo="target1" />

Link to the documentation.

Keble answered 27/8, 2021 at 5:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.