简体   繁体   中英

NLog: disable/enable multiple log files

I have 2 loggers: one for Info messages, and another for other ones.

<logger name="ErrLogger" levels="Trace,Debug,Warn,Fatal,Error" writeTo="logfile_w" />
<logger name="InfoLogger" level="Info" writeTo="logfile_i" />

Each one write information to his own file. Is it possible in runtime turn off and on certain loggers?

Btw, for each class I'm using code like this:

private static Logger _logger = LogManager.GetCurrentClassLogger();

And then in functions I use _logger.Info("message"); . Should I modify class loggers too?

You can use filtering for that.

Example

<rules>
    <logger name="*" writeTo="file">
        <filters>
            <when condition="${logger}==loggername" action="Ignore" />

        </filters>
    </logger>
</rules>

See filtering on NLog wiki and the ${logger} layout renderer .

Hint: not sure what the (full) name of the logger is? Just log something with ${logger} and check the logs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM