2012-02-23 41 views
1

請考慮下面的代碼:全正在重置NLOG配置

public static void Main(string[] args) 
{ 
    // 1. At this point NLog will search for most common configuration locations, 
    // as described in http://nlog-project.org/wiki/Configuration_file. 

    var loggerOne = LogManager.GetLogger("LoggerOne"); 

    // 2. We set NLog configuration to null, hoping that the next time the 
    // configuration is requested the whole process (1) will be repeated. 

    LogManager.Configuration = null; 

    // 3. NLog won't actually repeat process (1), and so logging is disabled for 
    // the following logger since the configuration is still null. 

    var loggerTwo = LogManager.GetLogger("LoggerTwo"); 
} 

瀏覽NLOG的源代碼,很顯然,這與底層LogFactory設定在true在啓動configLoaded領域做的,在將null分配給Configuration屬性(這對我來說最有意義)之後,未被重置爲false。我的問題是我在這個過程中是否錯過了某些東西,或者是否確實如此(無需使用反射)來強制NLog從頭開始重新配置自身,並重復過程(1)

回答

0

首先,讓我問一下你試圖完成什麼?這更多的是試圖瞭解NLog的工作原理嗎?有什麼特別的東西想要完成?如果是後者,也許已經有了一個被認可的方式去做你想做的事情。

話雖如此,我有一個想法(我還沒有嘗試過),可能工作。

在看NLOG源,好像你也許能夠做這樣的事情:

LogManager.Configuration = new LoggingConfiguration(); 

分配一個新的LoggingConfiguration看起來像它會導致日誌記錄配置過程從頭開始。我不確定在舊配置生效時可能檢索到的任何記錄器會發生什麼情況。他們仍然有效嗎?他們會做任何事嗎?