我正在使用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();
我認爲這將是容易的,但configuration.Reload沒有做什麼,我期待,我沒有發現任何其他合適的方法。 調用之後,我的記錄器在程序啓動時仍不存儲跟蹤和調試事件。
所以我的問題是:如何以編程方式強制在運行時重新加載NLog.config並恢復我的「標準」日誌記錄器設置?
感謝
更多的細節問題更新:
我使用VS2013,.NET 4中謨與NLOG 4.4.0-beta11(測試階段,但最後上的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
在n日誌內部日誌,我可以看到的配置似乎已經重新裝入沒問題(很長的文件,所以我除非有人詢問,否則不要在這裏發佈),但也許現有的記錄器不會更新?
上面的代碼應該很容易在你身邊試試。請讓我知道如果我做錯了什麼或者它是否是NLog配置管理中的錯誤。
這是使用它的正確方法。檢查內部日誌,它會告訴重新加載時發生的情況。 – Julian
你好。很高興知道。明天,我將啓用Nlog內部日誌,並提供詳細信息並隨時通知您。謝謝 – AFract
你好@朱連。我已經在上面發佈了完全正確的測試,但仍然存在問題。你可以檢查一下嗎?非常感謝你,如果我認爲你是NLog的主要作者之一,我很高興幾年以來對我的所有項目都非常滿意。 – AFract