Initialize log4Net as early as possible with NUnit

后端 未结 2 1187
陌清茗
陌清茗 2020-12-20 04:02

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 a

2条回答
  •  清酒与你
    2020-12-20 04:35

    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);
      }
    }
    

    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 :(

提交回复
热议问题