Iworb
Iworb

Reputation: 532

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?

Upvotes: 0

Views: 606

Answers (1)

Julian
Julian

Reputation: 36710

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.

Upvotes: 1

Related Questions