问题描述
我正在将NLog与配置文件一起使用.为了避免在长时间的治疗中记录太多的东西,我正在做这种事情:
I'm using NLog with configuration files.To avoid to log too much things for some long treatments, I'm doing this kind of thing :
foreach (LoggingRule rule in LogManager.Configuration.LoggingRules)
{
rule.DisableLoggingForLevel(LogLevel.Trace);
rule.DisableLoggingForLevel(LogLevel.Debug);
}
LogManager.ReconfigExistingLoggers();
它可以让我暂时降低日志记录的详细程度.上面的几行仅是一个示例.这似乎工作得很好.
It allows me to temporarily degrade logging detail level. The lines above are just an example to illustrate.This seems to work perfectly.
在那之后,我想通过重新加载我的NLog.Config文件来恢复我的常规"日志记录配置,该文件已在程序启动时自动加载.我已经尝试过了:
After that, I would like to restore my "regular" logging configuration, simply by reloading my NLog.Config file which was already automatically loaded at program startup.I've tried :
LogManager.Configuration.Reload();
LogManager.ReconfigExistingLoggers();
我认为这很容易,但是配置.Reload并没有达到我的预期,也没有找到其他合适的方法.通话结束后,在程序启动时,我的记录器仍然不存储跟踪和调试事件.
I thought it would be easy but configuration.Reload doesn't do what I was expecting and I didn't find any other suitable method.After the call my logger still not store traces and debug events, while they were at program startup.
所以我的问题是:如何以编程方式在运行时强制重新加载NLog.config并恢复我的标准"记录器设置?
So my question is : how to programmatically force the reloading of NLog.config at runtime and restore my "standard" loggers settings ?
谢谢
具有更多详细信息的问题更新:
我使用的是VS2013,.Net 4 projet和NLog 4.4.0-beta11(测试版,但最后一次使用nuget),在配置文件中定义了简单的文件"目标,并使用minLevel = Trace和autoReload = True
I'm using VS2013, .Net 4 projet with NLog 4.4.0-beta11 (beta but last on nuget), simple "File" target defined in configuration file with minLevel = Trace and autoReload=True
在类级别实例化的记录器: 私有静态只读Logger Logger = LogManager.GetCurrentClassLogger();
Logger instantiated at class level : 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内部日志中,我可以看到配置似乎已被重新加载,没有问题(文件很长,因此除非有人询问,否则我不会将其发布在这里),但是也许现有的记录器未更新?
In the nlog internal log I can see the configuration seems to have been reloaded with no problem (very long file so I don't post it here unless somebody asks), but maybe the existing loggers are not updated ?
使用上面的代码,应该很容易在您身边尝试.如果我做错了什么,或者是NLog配置管理中的错误,请告诉我.
With the code above it should be easy to try on your side. Please let me know if I do something wrong or if it's a bug inside NLog configuration management.
推荐答案
实际上,您的代码中有一个错误:LogManager.Configuration.Reload()不会更改记录器的配置,但是会从NLog.config加载配置文件,将其反序列化并返回LoggingConfiguration作为结果.
Actually, you've got an error in your code: LogManager.Configuration.Reload() does not change configuration of logger, but loads configuration from NLog.config file, deserializes it and returns LoggingConfiguration as a result.
要恢复配置,您需要执行以下操作:
to restore configuration, you need to do something like this:
LogManager.Configuration = LogManager.Configuration.Reload();
LogManager.ReconfigExistingLoggers();
这是一个测试示例(示例config中的minLevel ="DEBUG"):
here is a test example (minLevel in example config = "DEBUG"):
[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
这篇关于如何强制重新加载NLog配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!