Unit Testing: Logging and Dependency Injection

后端 未结 9 591
臣服心动
臣服心动 2021-02-02 14:57

So regards logging from SO and other sites on the Internet the best response seems to be:

void DoSomething() {
    Logger.Log(\"Doing something!\");
    // Code.         


        
9条回答
  •  时光取名叫无心
    2021-02-02 15:47

    Although I agree with others that I wouldn't apply TDD to logging, I would try to ensure that unit testing covers all code paths that contain logging statements. And importantly ensure that the highest verbosity level is configured while running the unit tests, so that all logging statements are executed.

    For example, the following code has a bug which will throw a FormatException only if if Debug level tracing is enabled.

    if (logger.IsDebugEnabled)
    {
        string message = String.Format(CultureInfo.CurrentCulture, 
              "... {1} ...", someValue);
        logger.Debug(message);
    }
    

提交回复
热议问题