In common logging V2.0当 LogLevel 高于日志条目时,有两种方法可以避免消息评估的成本:
if (Log.IsDebugEnabled)
Log.Debug("Debug message");
或
Log.Debug(a => a("Debug message"));
哪种做法更好?有什么优缺点?
最佳答案
根据文档:
Leveraging lambdas, the ILog interface offers a new & safe way to write log statements
log.Debug( m=>m("value= {0}", obj.Value) );
This ensures, that the whole expression is only evaluated when LogLevel.Debug is enabled and thus saves you from having to write
if (log.IsDebugEnabled)
{
log.Debug("value={0}", obj.Value);
}
to avoid this overhead.
因此您问题中的第二个选项被认为是最佳实践。
关于.net - IsDebugEnabled 与 Debug(Action<FormatMessageHandler>),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4667103/