我使用Nlog记录日志,发现对nLog的每次调用都在日志文件中有5个条目。我该如何解决?
例如

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


我的nLog配置如下

/****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*****/


您可以看到打印了6次starup,并且在Ninjatrader onStartup中,在程序开始时该程序仅被调用一次。

我也拥有范围广泛的Global Diagnistoc,但我不认为这会导致这种情况的发生

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);
  }
}

最佳答案

如果您有多个针对实例文件的规则,则将对其进行多次写入。

关于logging - 重复的日志条目Nlog,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14020208/

10-13 06:30