How to log to multiple targets with different logging levels using NLog?

11,040

Solution 1

I just found the solution to log multiple targets programmatically.

I just don't use the SimpleConfigurator but the LogManager to set the NLog.LoggingConfiguration to its property.

So i came out with following code snippet:

// define the targets
// ......

// create configuration object and set previously created targets
LoggingConfiguration configuration = new LoggingConfiguration();
configuration.AddTarget("methodCall", methodCallTarget);
configuration.AddTarget("logfile", fileTarget);

// create logging rules where i can specify the minimum log levels
// and add them to the configuration objects LoggingRules Enumerable.
LoggingRule logFileRule = new LoggingRule("*", NLog.LogLevel.Debug, fileTarget);
configuration.LoggingRules.Add(logFileRule);

LoggingRule methodCallRule = new LoggingRule("*", NLog.LogLevel.Info, methodCallTarget);
configuration.LoggingRules.Add(methodCallRule);

// Finally set the configuration object to the LogManagers Configuration property
LogManager.Configuration = configuration;

Thanks for your answers!

Solution 2

You should be able to specify the same logger and send it to two different targets.

Assuming you have configured targets "f1" for the file and "m1" for the method, you should able to configure the loggers like this:

<logger name="*" minlevel="Trace" writeTo="f1" />  
<logger name="*" minlevel="Debug" writeTo="m1" />  

This should send all logging messages to the file target, f1, and all Debug and higher messages to the method target, m1.

Also, see this question and its answers for more information on configuring NLog. You might find something useful.

I just Googled and found this post that seems to describe your problem and a solution:

http://nlog-forum.1685105.n2.nabble.com/Programatic-Configuration-of-targets-and-rules-td1685349.html

Maybe it will help.

Solution 3

SimpleConfigurator overwrites all existing rules. In your example, you had 2 calls, so the first target got discarded. Instead, you should manually add a target and logging rule.

Share:
11,040

Related videos on Youtube

dasheddot
Author by

dasheddot

Updated on June 04, 2022

Comments

  • dasheddot
    dasheddot almost 2 years

    I run into the same issue described there: Another StackOverflow Question

    I need to log to file and method calling. The problem is that there are lot of "debug" level logging messages which are more detailled. I need them to log to file. But the method should only receive logging messages above debug level.

    So the SplitGroupTarget doesn't fullfill my requirements. Is there any solutions or workarounds for this problem?

    Additionally i found this entry in the NLog forum with a similiar problem in 2006 - but with no answer yet: NLog Forum

    EDIT1: I forgot to mention that i want to configure this programmatically. According to your answer i tried it in the following way, but only the last target gets logged.

    SimpleConfigurator.ConfigureForTargetLogging(methodCallTarget, LogLevel.Debug);
    SimpleConfigurator.ConfigureForTargetLogging(fileTarget, LogLevel.Debug);
    
  • dasheddot
    dasheddot over 12 years
    Thanks for your response, but i tried to do it programmatically, this way it does not work either. (see my edit)