2012-02-23 39 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看起来像它会导致日志记录配置过程从头开始。我不确定在旧配置生效时可能检索到的任何记录器会发生什么情况。他们仍然有效吗?他们会做任何事吗?