如何强制重新加载NLog配置文件?

我正在使用NLog配置文件。 为了避免为长时间的治疗记录过多的东西,我正在做这样的事情:

foreach (LoggingRule rule in LogManager.Configuration.LoggingRules) { rule.DisableLoggingForLevel(LogLevel.Trace); rule.DisableLoggingForLevel(LogLevel.Debug); } LogManager.ReconfigExistingLoggers(); 

它允许我暂时降低日志记录详细信息级别。 上面的几行只是一个例子来说明。 这似乎完美无缺。

之后,我想恢复我的“常规”日志记录配置,只需重新加载已在程序启动时自动加载的NLog.Config文件 。 我试过了 :

 LogManager.Configuration.Reload(); LogManager.ReconfigExistingLoggers(); 

我认为这很容易但配置.Reload没有做我期待的事情,我没有找到任何其他合适的方法。 在调用之后,我的记录器在程序启动时仍然没有存储跟踪和调试事件。

所以我的问题是:如何以编程方式强制在运行时重新加载NLog.config并恢复我的“标准”记录器设置?

谢谢

有关详细信息的问题更新:

我正在使用VS2013,.Net 4 projet和NLog 4.4.0-beta11(beta但是最后一个在nuget上),简单的“文件”目标在配置文件中定义,minLevel = Trace和autoReload = True

Logger在类级别实例化:private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

这是我的测试证据:

  Assert.True(Logger.IsTraceEnabled); Assert.True(Logger.IsDebugEnabled); Logger.Trace("1 - This should appear in log"); Logger.Debug("2 - This should appear in log"); foreach (LoggingRule rule in LogManager.Configuration.LoggingRules) { rule.DisableLoggingForLevel(LogLevel.Trace); rule.DisableLoggingForLevel(LogLevel.Debug); } LogManager.ReconfigExistingLoggers(); Logger.Trace("3 - This should NOT appear in log"); Logger.Debug("4 - This should NOT appear in log"); Logger.Info("5 - This should appear in log"); Assert.False(Logger.IsDebugEnabled); Assert.False(Logger.IsTraceEnabled); LogManager.Configuration.Reload(); LogManager.ReconfigExistingLoggers(); // This is were something goes wrong Logger.Trace("6 - This should appear in log"); // DOES NOTHING Logger.Debug("7 - This should appear in log"); // DOES NOTHING Logger.Info("8 - This should appear in log"); Assert.True(Logger.IsDebugEnabled); // FAILS Assert.True(Logger.IsTraceEnabled); // FAILS 

生成的日志:

  TRACE 1 - This should appear in log DEBUG 2 - This should appear in log INFO 5 - This should appear in log INFO 8 - This should appear in log 

在nlog内部日志中,我可以看到配置似乎已经重新加载没有问题(非常长的文件所以我不在这里发布,除非有人问),但是现有的记录器可能没有更新?

使用上面的代码,您应该很容易尝试。 如果我做错了或Nlog配置管理中存在错误,请告诉我。

实际上,您的代码中出现错误:LogManager.Configuration.Reload()不会更改logger的配置,但会从NLog.config文件加载配置,对其进行反序列化并返回LoggingConfiguration。

要恢复配置,您需要执行以下操作:

 LogManager.Configuration = LogManager.Configuration.Reload(); LogManager.ReconfigExistingLoggers(); 

这是一个测试示例(示例config =“DEBUG”中的minLevel):

 [TestMethod] public void Test() { Logger.Trace("TRACE 1"); Logger.Debug("DEBUG 1"); Logger.Warn("WARN 1"); foreach (LoggingRule rule in LogManager.Configuration.LoggingRules) { rule.DisableLoggingForLevel(LogLevel.Trace); rule.DisableLoggingForLevel(LogLevel.Debug); } LogManager.ReconfigExistingLoggers(); Logger.Trace("TRACE 2"); Logger.Debug("DEBUG 2"); Logger.Warn("WARN 2"); // Reconfigure(); LogManager.Configuration = LogManager.Configuration.Reload(); LogManager.ReconfigExistingLoggers(); Logger.Trace("TRACE 3"); Logger.Debug("DEBUG 3"); Logger.Warn("WARN 3"); } 

输出:

 (DEBUG): DEBUG 1 (WARN): WARN 1 // here settings changed (WARN): WARN 2 //here settings reloaded (DEBUG): DEBUG 3 (WARN): WARN 3