junkone
junkone

Reputation: 1465

duplicate log entries Nlog

I use Nlog to log and i find that i have 5 entries in my log file for every call made to nLog. How can i fix it? for eg.

2012-12-23 18:18:19.2465 NinjaTrader.Strategy.LODHOD.OnStartUp Debug   startup
2012-12-23 18:18:19.2465 NinjaTrader.Strategy.LODHOD.OnStartUp Debug   startup
2012-12-23 18:18:19.2465 NinjaTrader.Strategy.LODHOD.OnStartUp Debug   startup
2012-12-23 18:18:19.2465 NinjaTrader.Strategy.LODHOD.OnStartUp Debug   startup
2012-12-23 18:18:19.2465 NinjaTrader.Strategy.LODHOD.OnStartUp Debug   startup
2012-12-23 18:18:19.2465 NinjaTrader.Strategy.LODHOD.OnStartUp Debug   startup

My nLog config is as below

/****CLASS LEVEL VARIABLES***/
// Step 1. Create configuration object 
private static LoggingConfiguration config = new LoggingConfiguration();
private static FileTarget fileTarget = new FileTarget();
private static Logger logger = LogManager.GetCurrentClassLogger();
/*** END OF CLASS LEVEL VARIABLES ***/

/*************************NLOG CONFIG*****/

fileTarget.FileName = 
  "C:\\temp\\" + Instrument.FullName + "nLog." + DateTime.Now.Ticks + ".log";
fileTarget.Layout = 
  "${longdate} ${callsite} ${level} ${event-context:item=StrategyId}  ${message}";

config.AddTarget("file", fileTarget);

// Step 4. Define rules
LoggingRule rule2 = new LoggingRule("*", NLog.LogLevel.Trace, fileTarget);
config.LoggingRules.Add(rule2);

// Step 5. Activate the configuration
LogManager.Configuration = config;


logger.Debug("startup");


/*************************NLOG CONFIG*****/

you can see the starup printed 6 times and it is called only once in the program at the begining of the program in the Ninjatrader onStartup.

I also have the scoped Global Diagnistoc but i dont think that can cause this to happen

public class ScopedGlobalContext : IDisposable
{
  private string n;
  private string v;

  public ScopedGlobalContext(string name, string value)
  {
    n = name;
    v = value;
    NLog.GlobalDiagnosticsContext.Set(n, v);
  }

  public void Dispose()
  {
    NLog.GlobalDiagnosticsContext.Remove(n);
  }
}   

Upvotes: 2

Views: 6672

Answers (6)

Rolf Kristensen
Rolf Kristensen

Reputation: 19942

NLog.Web.AspNetCore v4.8.6 changes registration of NLog Logging Provider, so one is now allowed to call both AddNLog and UseNLog without experiencing double logging.

NLog 5.0 implements validation of incorrect duplicate target configuration, so one will not experience double logging when using incorrect NLog configuration (Will also notify in NLog InternalLogger)

Upvotes: 0

nPcomp
nPcomp

Reputation: 9983

In my case, marking the rules as final solved the issue.

<rules>
   <logger name="*" minlevel="Warn"  appendTo="database" final="true"/>
   <logger name="*" minlevel="Fatal" appendTo="database" final="true"/>
   <logger name="*" minlevel="Error" appendTo="database" final="true"/>
   <logger name="*" minlevel="Info"  appendTo="database" final="true"/>
   <logger name="*" minlevel="Debug" appendTo="database" final="true"/>
   <logger name="*" minlevel="Trace" appendTo="database" final="true"/>
</rules>

Upvotes: 4

ATL_DEV
ATL_DEV

Reputation: 9591

I had the same thing happen to me. Several searches lead to other people having the same issue, but no solution. I think this is a bug that happens if you're running multiple threads and/or if code doing the logging is being called by a Linq query. Remember, Linq does not always execute immediately or once.

Upvotes: 2

mohamed amine
mohamed amine

Reputation: 3

adding final attribute to logger solved my issue

<logger name="*" minlevel="Info" writeTo="log" final="true"/>

Upvotes: 0

willll
willll

Reputation: 1839

WhenRepeated-Filter

Matches when the calculated layout has already been logged. Useful if having an aggressive logger, and wants to throttle the output.

Introduced with NLog ver. 4.5

It looks to be the solution introduced by nlog to debounce/throttle log outputs.

Upvotes: 1

Johan
Johan

Reputation: 753

If you have multiple rules that targets for instance file, then you get multiple writes to it.

Upvotes: 5

Related Questions