TAGS :Viewed: 12 - Published at: a few seconds ago

[ NLog: How to send log message to console from two different targets without duplications? ]

I have three targets - Engine, Tasks and Error. Please, find belog their NLog configuration:

<targets>
  <target name="EngineLog" xsi:type="File" fileName="C:\Log\EngineLog.txt" layout="${layout}"/>
  <target name="ErrorLog" xsi:type="File" fileName="C:\Log\ErrorLog.txt" layout="${layout}"/>
  <target name="TasksLog" xsi:type="File" fileName="C:\Log\TasksLog.txt" layout="${layout}"/>
  <target name="ConsoleLog" xsi:type="ColoredConsole" layout="${consoleLayout}"/>
</targets>
<rules>
  <logger name="*" minLevel="Error" writeTo="ErrorLog"/>

  <logger name="N1.*" minLevel="Warn" writeTo="EngineLog" final="true"/>
  <logger name="N2.*" minLevel="Info" writeTo="EngineLog" final="true" />
  <logger name="N3.*" minLevel="Info" writeTo="EngineLog" final="true" />
  <logger name="N4.*" minLevel="Info" writeTo="EngineLog" final="true" />
  <logger name="N5.*" minLevel="Info" writeTo="EngineLog" final="true" />

  <logger name="N6" minLevel="Info" writeTo="EngineLog" />
  <logger name="*" minlevel="Info" writeTo="TasksLog" />
</rules>

Of course, the real namespace names are not N1...N6, what matters is that I have:

  • 5 namespaces which are logged to the Engine log exclusively (N1-N5)
  • All the errors (both Engine and Tasks) are logged to the same Error log in addition to the respective dedicated target.
  • One namespace is logged both to Engine and Tasks (N6)
  • The rest is considered Tasks

Now I would like additionally to log everything going to either ErrorLog or TasksLog to the console.

My first try was to wrap the ErrorLog and TasksLog with a SplitGroup grouping each target with the console target, like this:

<target name="ErrorLog" xsi:type="SplitGroup">
  <target xsi:type="File" fileName="C:\Log\ErrorLog.txt" layout="${layout}"/>
  <target xsi:type="ColoredConsole" layout="${consoleLayout}"/>
</target>
<target name="TasksLog" xsi:type="SplitGroup">
  <target xsi:type="File" fileName="C:\Log\TasksLog.txt" layout="${layout}"/>
  <target xsi:type="ColoredConsole" layout="${consoleLayout}"/>
</target>

But this is a wrong approach, because every error gets logged twice on the console - first on behalf of the ErrorLog and then on behalf of the TasksLog.

How can I log errors to the console without duplications while allowing them to go to both the ErrorLog target and the TasksLog/EngineLog targets?

EDIT

The desired effect can be achieved if I could specify to log messages, but not if they are Error or Fatal (because these have already been logged).

Answer 1


OK, I think I have found the solution:

<target name="ErrorLog" xsi:type="SplitGroup">
  <target xsi:type="File" fileName="C:\Log\ErrorLog.txt" layout="${layout}"/>
  <target name="ConsoleLog" xsi:type="ColoredConsole" layout="${consoleLayout}"/>
</target>

And add the following rule at the end:

<logger name="*" levels="Warn, Info, Debug, Trace" writeTo="ConsoleLog" />