c# - Initialize log4Net as early as possible with NUnit -
i'm wondering best way initialize log4net in nunit project. of course want call init code (ie. xmlconfigurator.configure()
) can many log output can. since project run through nunit, have little control on entry point.
according nunit documentation, should call constructors first, method marked [setup]
attribute in class marked [testfixturesetup]
.
so, first, created static helper class 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; } }
then, made [testfixturesetup]
inherit single 1 pretty nothing else calling loggingfacility.initlogger()
. still leaves constructors run earlier, in order can assume random. , moreover, static initializations before able execute code.
in fact, can see in log, first 4 seconds or of execution unrecorded.
does mean have call initlogger()
in every constructor , forbid use of static initializer? that's tough job!
does know magic trick this?
for single initialization point should use class marked [setupfixture]
attribute , method marked [setup]
, example:
[setupfixture] public class testsinitializer { [setup] public void initializelogger() { loggingfacility.initlogger(); } }
now, method ([setup] initializelogger
) run before test run, same 1 marked [teardown]
run once tests run. here's catch - any , all mean in context? tests classes declared in same namespace class marked [setupfixture]
.
for example, assuming hierarchy this:
- tests --- business ----- testsinitializer.cs // setupfixture class ----- firstbusinesstests.cs ----- secondbusinestests.cs --- complexlogic ----- verycomplexlogictests.cs
first
, secondbusinesstests
run after setup
testsinitializer
, verycomplexlogictests
might run in random order.
according linked documentation, if declare setupfixture
class outside of namespace, setup , teardown apply entire assembly:
only 1 setupfixture should created in given namespace. setupfixture outside of namespace provides setup , teardown entire assembly.
Comments
Post a Comment