1

I am trying to create 2 loggers (using NLog)

  1. First logger logs all the desired item to log in the solution
  2. The other one traces specific items (I do it to keep things clean and focused, and only run trace here)

Below is the configuration

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
  <target name="logfile"
          xsi:type="File"
          layout="${longdate} ${level} ${threadid} ${callsite} ${message}"
          fileName="${basedir}\Logs\GatewayApplicationDebugAndErrorLog.txt"
          archiveNumbering="Rolling"
          maxArchiveFiles="10"
          archiveAboveSize="10000000"/>     
  <target name="J1939Trace"
          xsi:type="File"
          layout="${longdate} ${level} ${threadid} ${callsite} ${message}"
          fileName="${basedir}\Logs\J1939Trace.txt"
          archiveNumbering="Rolling"
          maxArchiveFiles="10"
          archiveAboveSize="10000000"/>
</targets>
<rules>
  <logger name="*" minlevel="Trace" writeTo="logfile" />
  <logger name="J1939Trace" maxlevel="Trace" writeTo="J1939Trace" final="true" />
</rules>

and usage is shown below

private readonly Logger logger = LogManager.GetCurrentClassLogger(); // Generic Logger
private readonly Logger j1939Logger = LogManager.GetLogger("J1939Trace"); // Specific Logger.

What I observe is that the specific logger item is also logged in generic log item and I don't want that duplication. Any ideas what am I doing wrong?

Julian
  • 33,915
  • 22
  • 119
  • 174
Abhishek Kumar
  • 342
  • 5
  • 22
  • Is the intention that the one log is trace, and the other log is above trace? Could you just change the generic logger to be at debug? – Derek Jun 29 '17 at 10:59
  • @derek That is not the intention. The intention id to log every thing except what is logged in specific logger. – Abhishek Kumar Jun 29 '17 at 11:02

1 Answers1

1

Will this work?

From: NLog: How to exclude specific loggers from a specific rule?

Adding final="true" means that no more rules will be executed for the events produced by "SpammyLogger", but it applies only to the specified levels.(see https://github.com/nlog/nlog/wiki/Configuration-file#rules)

Make sure to read through all the comments in the answer.

Derek
  • 7,615
  • 5
  • 33
  • 58