How to force reload of NLog configuration file?

给你一囗甜甜゛ 提交于 2019-12-01 18:37:46

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

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

output:

(DEBUG): DEBUG 1
(WARN): WARN 1
// here settings changed
(WARN): WARN 2
//here settings reloaded
(DEBUG): DEBUG 3
(WARN): WARN 3
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!