LogWriter.ShouldLog(LogEntry) 方法行为基于什么?

发布于 2024-10-31 00:13:43 字数 696 浏览 4 评论 0原文

什么是 LogWriter.ShouldLog(..) 方法行为基于?或者说它的真正用途是什么?它的文档页面相当稀疏,并且没有提供太多的见解。引用它:

查询 LogEntry 是否应该[原文如此] 已记录。

在所有版本的企业库中都出现相同的拼写错误,让我想知道作者是否非常重视它。

这个方法对我来说似乎相当空灵。我应该在登录之前检查它吗?

目前我只检查 LogWriter.IsLoggingEnabled(..) 它基于配置中的显式设置。这代表了一个具体的场景:日志记录要么打开,要么关闭。

What is the LogWriter.ShouldLog(..) method behaviour based on? Or what is the real intention of its usage? Its documentation page is rather sparse and doesn't provide much insight. Quoting it:

Queries whether a LogEntry shold[sic] be
logged.

(Complete with same typo in all versions of the Enterprise Library, makes me wonder if the authors paid much attention to it.)

The method seems rather ethereal to me. Should I always check it before logging?

Currently I only check LogWriter.IsLoggingEnabled(..) which is based on an explicit setting in configuration. This represents a concrete scenario: the logging is either turned on or off.

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

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

发布评论

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

评论(1

岁月如刀 2024-11-07 00:13:43

行为

LogWriter.ShouldLog( LogEntry logEntry ) 根据 LogEntry 中的数据查询所有已配置的过滤器,以确定是否应记录特定 LogEntry。如果所有过滤器都返回 true(对于 ShouldLog),则调用 Write 方法时将记录 LogEntry。尽管您可以创建自己的自定义过滤器,但开箱即用的过滤器有“类别”、“优先级”和“启用日志记录”。

相比之下,IsLoggingEnabled 检查仅检查一个过滤器,而 ShouldLog 调用则检查所有过滤器(包括 IsLoggingEnabled)。

意图

调用的目的是让开发人员在 LogEntry 不被记录时避免昂贵的操作。 “昂贵”的程度取决于应用和要求。例如,在紧密循环中进行过多的字符串操作,或者可能进行进程外调用来检索某些信息(尽管这可能是缓存的一个很好的候选者!)。

我应该在登录之前检查它吗?

一般来说,我会避免调用 ShouldLog 除非有令人信服的理由不这样做。

我可以想到几个原因:

  1. 它使代码变得混乱
  2. 如果您尝试使用辅助方法来整理代码,那么 LogEntry 通常会被完全填充,因此您可能无论如何都无法避免任何操作(尽管您可以传入一个委托而不是调用它,但我不确定这会让事情变得更简单)
  3. 在内部,Write 方法已经调用 ShouldLog 所以如果你正在记录,那么 ShouldLog 将被调用每条记录的消息两次

Behavior

LogWriter.ShouldLog( LogEntry logEntry ) queries all of the configured filters against the data in the LogEntry to determine if the specific LogEntry should be logged. If all filters return true (for ShouldLog) then the LogEntry would be logged if the Write method were called. The out of the box filters are Category, Priority, and LoggingEnabled although you can create your own custom filters.

The IsLoggingEnabled check in contrast just checks one filter whereas the ShouldLog call checks all filters (including IsLoggingEnabled).

Intention

The intention of the call is to allow the developer to avoid expensive operations if the LogEntry will not be logged. What "expensive" is will depend on the application and requirements. e.g. excessive string manipulation in a tight loop or perhaps an out of process call to retrieve some information (although that might be a good candidate for caching!).

Should I always check it before logging?

In general, I would avoid calling ShouldLog unless there is a compelling reason to do otherwise.

I can think of a few reasons:

  1. It clutters the code
  2. If you try to de-clutter the code with a helper method then the LogEntry would usually be fully populated so you probably wouldn't have avoided any operations anyway (although you could pass in a delegate and not invoke it but I'm not sure that is making things simpler)
  3. Internally, the Write method already calls ShouldLog so if you are logging then ShouldLog will be called twice for every message logged
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文