为什么在log4net中调用IsDebugEnabled?

发布于 2024-10-06 09:32:22 字数 357 浏览 0 评论 0原文

我很好奇为什么我看到人们编写如下所示的 log4net 日志记录代码:

if (_logger.IsDebugEnabled)
{
    _logger.Debug("Some debug text");
}

我已经完成了 log4net 的反汇编,并且调用 Debug 会再次调用相同的代码以查看它在实际日志记录之前是否已启用,因此 IsDebugEnabled call 是不必要的,实际上是重复的代码。

人们这样做有理由吗?也许是旧版本中曾经必需的旧模式,但现在不再需要了?或者是否有合理的理由?或者也许人们只是不知道他们不需要这样做?

其他级别(信息、错误、警告、最佳等)也存在相同的行为。

I'm curious as to why I see people write log4net logging code like the following:

if (_logger.IsDebugEnabled)
{
    _logger.Debug("Some debug text");
}

I've gone through the disassembly for log4net, and calling Debug makes another call to the same code to see if it's enabled before actually logging, so the IsDebugEnabled call is unnecessary and actually is duplicated code.

Is there a reason people do this? Maybe an old pattern that used to be necessary in older versions but isn't anymore? Or could there be a legitimate reason for it? Or maybe people just don't know that they don't need to do it?

This same behavior is there for the other levels (Info, Error, Warn, Finest, etc.) as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美人如玉 2024-10-13 09:32:22

使用此模式纯粹是出于性能原因,特别是当由于当前未启用日志记录级别而将跳过特定日志记录级别的日志记录时。检查布尔 IsDebugEnabled 标志并跳过方法调用比使用参数调用 Debug 方法和不记录日志的返回方法要便宜得多。

如果您要调用 Debug 方法并传入一条包含创建成本较高的内容的消息,则可以通过首先检查启用标志来完全跳过消息的创建。

话虽这么说,除非您正在制作非常昂贵的日志消息(例如堆栈跟踪之类的东西)或者您正在紧密循环中进行日志记录,否则它不太可能成为您的代码的瓶颈。

This pattern is used purely for performance reasons, specifically when logging to a certain logging level will be skipped because the logging level is not currently enabled. It is a lot cheaper to check the boolean IsDebugEnabled flag and skip the method call than it is to call the Debug method with arguments and the method to return without logging.

If you were to call the Debug method and pass in a message containing something that was costly to create, you could skip the creation of the message altogether by first checking the enabled flag.

All that being said, unless you are making log messages that are very expensive to make (eg. something like a stack trace) or you are logging in a tight loop, it is unlikely that it will be a bottleneck for your code.

锦欢 2024-10-13 09:32:22

该消息的构建成本可能很高。将其包装在 if 语句中可确保仅在必要时创建它。

解决此问题的另一种模式是:

_logger.Debug(() => "Some expensive text");

不过,我不知道 log4net 是否支持类似的内容。

The message could be expensive to build. Wrapping it in the if statement ensures it is only created when necessary.

Another pattern that addresses this issue is:

_logger.Debug(() => "Some expensive text");

I don't know if log4net supports anything like that, though.

微凉徒眸意 2024-10-13 09:32:22

对于某些操作,您可能需要进行计算或额外检查才能输出所需的日志消息。

如果日志级别设置为高于 DEBUG,有时最好进行检查,而不是执行所有这些工作,从而防止在不记录日志的情况下执行额外的工作。

一个例子是页面加载时间,在调试模式下可以记录它们,但是如果不在调试级别,则不应创建秒表并且应跳过计算。

For some operations you might need to do calculation or extra checks to be able to output the required log message.

Rather than do all this work if the log level is set higher than DEBUG it can sometimes be better to check, therefore preventing the extra work being done if it won't be logged anyway.

An example could be page load times, in debug mode they can be logged, however if not at the DEBUG level, the stop watch shouldn't be created and the calculation should be skipped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文