初始化log4net的尽早与NUnit的 [英] Initialize log4Net as early as possible with NUnit
问题描述
我想知道什么是在NUnit的项目初始化log4net的最好的方式。当然,我要调用的初始化代码(即 XmlConfigurator.Configure()
),只要我能得到尽可能多的早日志输出,我可以。但是,因为我的项目是通过NUnit的运行,我有它的入口点几乎没有控制。
I'm wondering what is the best way to initialize log4Net in a NUnit project. Of course I want to call the init code (ie. XmlConfigurator.Configure()
) as soon as I can to get as many early log output as I can. But since my project is run through NUnit, I have little control on its entry point.
据NUnit的文档,它应该首先调用一些构造函数,然后打上一个方法在类 [设置]
属性标记 [TestFixtureSetup]
。
According to NUnit documentation, it should call some constructors first, then a method marked with the [SetUp]
attribute in a class marked with [TestFixtureSetup]
.
所以,首先,我创建了一个静态辅助类,我可以叫了几次都没有麻烦。
So, First, I created a static helper class which I can call several times without trouble.
public static class LoggingFacility
{
private static bool _loggerIsUp = false;
public static void InitLogger()
{
if (_loggerIsUp == false)
XmlConfigurator.ConfigureAndWatch(f);
_loggerIsUp = true;
}
}
然后,我做了我所有的 [TestFixtureSetup]
继承一个单一,做几乎没有什么比别的调用 LoggingFacility.initLogger()
。但是仍然被前面跑,在订单我只能假设随机所有构造函数。而且,它可能会做一些静态初始化我甚至能够执行一些代码之前。
Then, I made all my [TestFixtureSetup]
inherit a single one that does pretty much nothing else than calling LoggingFacility.initLogger()
. But that still leaves all constructors that are run earlier, in an order I can only assume random. And moreover, it will probably do some static initializations before I am even able to execute some code.
事实上,我可以在我的日志中看到,第一4秒或因此执行的是完全没有记录。
In fact, as I can see in my log, the first 4 seconds or so of execution are completely unrecorded.
这是不是意味着我会打电话给我的 InitLogger()
每构造函数和禁止使用任何静态初始化的?这是艰难的工作!
Does it mean I will have to call my InitLogger()
in every constructor and forbid the use of any static initializer? That's tough job!
是否有人知道一个魔术与此?
Does somebody know a magic trick with this?
推荐答案
一个工作伙伴向我提供了以下解决方法,完成这项工作:
A work mate provided me with the following workaround, that does the job:
在我所有的类需要记录,我有如下记录器初始化
In all my classes that require logging, I had the following logger initialization
private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
我简单地将其改为单初始化
I simply changed it to a singleton initializer
private static readonly ILog Log = LoggingFacility.GetLoggerWithInit(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
/*** ... ***/
public static class LoggingFacility
{
private static bool _loggerIsUp = false;
public static ILog GetLoggerWithInit(Type declaringType)
{
if (_loggerIsUp == false)
XmlConfigurator.Configure(_log4NetCfgFile);
_loggerIsUp = true;
return LogManager.GetLogger(declaringType);
}
}
由于我在每一个类的代码,这种静态。初始化必须由NUnit的诡计很早就叫我实例化测试类
Because I have this code in every class, this static initializer has to be called very early by NUnit wile instantiating my test classes.
下一步是使线程安全的:(
Next step is to make that thread safe :(
这篇关于初始化log4net的尽早与NUnit的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!