.net - IsDebugEnabled 与 Debug(Action<FormatMessageHandler>)

标签 .net logging lambda

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/

相关文章:

c# - 验证多个控件 Asp.Net

python - Python 中跨记录器的嵌套前缀

java - 将特定的 SLF4J 记录器与嵌入的 Jetty 相关联

java - Java 8 是否支持本地 lambda 变量?

python - 获取字典中某些值中最大值的键

.net - 从 XmlDocument 到 XmlDocument 的 XslCompiledTransform

c# - 为什么 SqlParametersCollection 每次都要求参数名称开头为 "@"?

c# - 扩展方法解决方案

spring - 如何在logback中使用请求参数作为日志标识符

javascript - Java 8 lambda(来自 javascript 示例)