LogWriter.ShouldLog(LogEntry) 方法行为基于什么?
什么是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
行为
LogWriter.ShouldLog( LogEntry logEntry ) 根据 LogEntry 中的数据查询所有已配置的过滤器,以确定是否应记录特定 LogEntry。如果所有过滤器都返回 true(对于 ShouldLog),则调用
Write
方法时将记录 LogEntry。尽管您可以创建自己的自定义过滤器,但开箱即用的过滤器有“类别”、“优先级”和“启用日志记录”。相比之下,
IsLoggingEnabled
检查仅检查一个过滤器,而ShouldLog
调用则检查所有过滤器(包括IsLoggingEnabled
)。意图
调用的目的是让开发人员在
LogEntry
不被记录时避免昂贵的操作。 “昂贵”的程度取决于应用和要求。例如,在紧密循环中进行过多的字符串操作,或者可能进行进程外调用来检索某些信息(尽管这可能是缓存的一个很好的候选者!)。我应该在登录之前检查它吗?
一般来说,我会避免调用
ShouldLog
除非有令人信服的理由不这样做。我可以想到几个原因:
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 theWrite
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 theShouldLog
call checks all filters (includingIsLoggingEnabled
).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:
ShouldLog
so if you are logging thenShouldLog
will be called twice for every message logged