1

I have a C# console app that is using NLog. In that app, I'm trying to log information to file locations conditionally. As a contrived example, let's pretend I need to sometimes log entries to an "evens" log file and sometimes I need to log entries to an "odds" log file. In an attempt to do this, I've setup the following configuration:

<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target name="evensLogFile" xsi:type="File" fileName="${basedir}/logs/evens.log" createDirs="true" keepFileOpen="true" encoding="utf-8" layout="${longdate} ${message}${exception:format=ToString}"></target>
    <target name="oddsLogFile" xsi:type="File" fileName="${basedir}/logs/odds.log" createDirs="true" keepFileOpen="true" encoding="utf-8" layout="${longdate} ${message}${exception:format=ToString}"></target>
  </targets>

  <rules>
    <logger name="Evens" minlevel="Trace" writeTo="evensLogFile" />
    <logger name="Odds" minlevel="Trace" writeTo="oddsLogFile" />
  </rules>
</nlog>

Now, in my code, when I want to write a log entry, I have the following:

public void Log(string message)
{
  var random = new Random();
  var i = random.Next(1, 11);

  Logger logger = null;
  if(i%2 == 0) {
    // somehow get a logger for "evens" 
  } else {
    // somehow get a logger for "odds"
  }
  logger.Log(LogLevel.Info, message);
}

I can successfully write logs to a single log file if I use Logger logger = LogManager.GetCurrentClassLogger(); However, I can't figure out how to log different log messages to different files based on some logic. Any help is appreciated!

user687554
  • 10,663
  • 25
  • 77
  • 138

2 Answers2

3

I googled "NLog multiple file targets" and this is what I came across. The following code should work with your current config.

class Program
{
    static void Main(string[] args)
    {
        Log("message 1");
        Log("message 2");
        Log("message 3");
        Log("message 4");
    }

    public static void Log(string message)
    {
        var random = new Random();
        var i = random.Next(1, 11);

        Logger logger = null;
        if (i % 2 == 0)
        {
            logger = LogManager.GetLogger("Evens");
        }
        else
        {
            logger = LogManager.GetLogger("Odds");
        }
        logger.Trace($"{message} - {i}");
    }
}

nlog.config

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target name="evensLogFile" xsi:type="File" fileName="${basedir}/logs/evens.log" createDirs="true" keepFileOpen="true" encoding="utf-8" layout="${longdate} ${message}${exception:format=ToString}"></target>
    <target name="oddsLogFile" xsi:type="File" fileName="${basedir}/logs/odds.log" createDirs="true" keepFileOpen="true" encoding="utf-8" layout="${longdate} ${message}${exception:format=ToString}"></target>
  </targets>

  <rules>
    <logger name="Evens" minlevel="Trace" writeTo="evensLogFile" />
    <logger name="Odds" minlevel="Trace" writeTo="oddsLogFile" />
  </rules>
</nlog>

odds.log

2018-04-24 19:50:04.9421 message 2 - 3

2018-04-24 19:50:05.1211 message 3 - 9

evens.log

2018-04-24 19:50:04.7572 message 1 - 8

2018-04-24 19:50:05.3137 message 4 - 6

Edit: used target instead of logger name. Edit2: added program.cs, nlog.config, output logs

Community
  • 1
  • 1
Matthew Thurston
  • 720
  • 5
  • 22
  • When I use this approach, the same message gets logged to both files (Evens AND Odds). However, I only want to log the message to one log file (Evens OR Odds). How do I log a message to only one of the log file targets? – user687554 Apr 24 '18 at 17:47
  • @user687554 do you incidentally have the configuration defined in more than one place? I tried using a sample program (added above) and it logged random even numbers to evens.log and random odd numbers to odds.log – Matthew Thurston Apr 24 '18 at 23:59
2

You can get a log by name, for example:

  if(i%2 == 0) {
    logger = LogManager.GetLogger("Evens");
  } else {
    logger = LogManager.GetLogger("Odds");
  }
Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
Lennart Stoop
  • 1,649
  • 1
  • 12
  • 18
  • When I use this approach, the same message gets logged to both files (Evens AND Odds). However, I only want to log the message to one log file (Evens OR Odds). How do I log a message to only one of the log file targets? – user687554 Apr 24 '18 at 17:47