什么是基于LogWriter.ShouldLog(LogEntry)方法的行为吗? [英] What is the LogWriter.ShouldLog(LogEntry) method behaviour based on?

查看:131
本文介绍了什么是基于LogWriter.ShouldLog(LogEntry)方法的行为吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是<一个href="http://msdn.microsoft.com/en-us/library/microsoft.practices.enterpriselibrary.logging.logwriter.shouldlog%28v=pandp.50%29.aspx"><$c$c>LogWriter.ShouldLog(..)基于法的行为呢?或者什么是它的使用的真实意图?它的文档页面是相当稀疏,不提供很大的启示。引用它:

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:

查询一个LogEntry是否shold [原文]是   记录下来。

Queries whether a LogEntry shold[sic] be logged.

完成与同错字在所有版本的企业库中,使我怀疑作者给予了极大的关注。的)

该方法似乎很空灵给我。如果日志记录之前,我总是检查呢?

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

目前我只检查<一href="http://msdn.microsoft.com/en-us/library/microsoft.practices.enterpriselibrary.logging.logwriter.isloggingenabled%28v=PandP.50%29.aspx"><$c$c>LogWriter.IsLoggingEnabled(..)这是基于在结构的明确的设置。这再presents一个具体的场景:日志或者是打开或关闭。

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.

推荐答案

LogWriter.ShouldLog(LogEntry logEntry)查询所有针对在LogEntry的数据配置的过滤器,以确定是否应该记录的具体LogEntry。如果所有的过滤器返回true(对于ShouldLog),则LogEntry是,如果方法被称为记录。的箱过滤器的输出是分类,优先级和LoggingEnabled虽然你可以创建自己的自定义过滤器。

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.

IsLoggingEnabled 检查对比只检查一个过滤器,而 ShouldLog 调用检查所有过滤器(包括 IsLoggingEnabled )。

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

通话的目的是让开发者避免昂贵的操作,如果 LogEntry 将不被记录。什么贵是将取决于应用和要求。例如过多的字符串操作在紧密循环或者是一个彻头彻尾的过程调用来检索一些信息(尽管这可能是一个很好的候选人缓存!)。

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!).

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

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

我能想到的几个原因:

  1. 这杂波的code
  2. 如果你试图去杂波code具有辅助方法,那么LogEntry通常会被完全填充,所以你可能不会回避任何操作呢(虽然你可以传递一个委托,而不是调用它但我不知道那是让事情变得更简单)
  3. 在内部,Write方法已经调用 ShouldLog 因此,如果您登录然后 ShouldLog 将每一个调用两次消息记录
  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

这篇关于什么是基于LogWriter.ShouldLog(LogEntry)方法的行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆