What are the best practices for including logging using log4net?

后端 未结 9 985
夕颜
夕颜 2021-02-07 04:04

I have been told to add “logging” to my code using log4net, the problem is no one can travel in time and see what real world problems the logging will need to be used to solve.<

9条回答
  •  悲&欢浪女
    2021-02-07 04:33

    One thing to bear in mind is that whilst your configuration will handle logging at different levels, you may be causing heavy overhead in your log calls. For example:

    // some kind of loop
    // do some operations
    Logger.LogDebug(myObject.GetXmlRepresentation());
    // end loop
    

    This will obviously only log the object if you have a logger listening to DEBUG logs, however the call to build the XML object will run regardless of your logging level and could cause some considerable slowdowns.


    The correct solution would be:

    // some kind of loop
    // do some operations
    if (Logger.IsDebug)
    {
        Logger.LogDebug(myObject.GetXmlRepresentation());
    }
    // end loop
    

提交回复
热议问题