Abhishek Kumar
Abhishek Kumar

Reputation: 352

Generic Logging and Specific Logging (using NLOG), Duplication of Logging

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?

Upvotes: 2

Views: 1045

Answers (1)

Derek
Derek

Reputation: 8793

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.

Upvotes: 2

Related Questions