企业库日志记录 - 如何在运行时获取配置的日志记录级别?
我们使用 Enterprise Library 4.1 进行日志记录(以及异常处理/加密)。
有谁知道在运行时确定配置的日志记录级别的好方法?我已经编写了一个 LogUtility 类来进行日志记录调用,并按照以下示例调用它:
LogUtility.LogVerbose(
string.Format("Processing event {0}", currentEvent.EventIDImported),
MethodBase.GetCurrentMethod().Name,
this.GetType().Name
);
我明白,除非在 app.config 中将日志记录级别设置为适当的级别,否则它实际上不会记录到文件中就我而言。但我真的不希望对方法参数(即方法和类型名称,以及在某些情况下记录的实际字符串)进行评估,除非绝对必要。
这看起来是一个合理的担忧吗?我们的应用程序可以有数千万次迭代和记录点。如果可能的话,我想根据配置的日志级别设置一个标志,并在进行上面的方法调用之前进行检查。
编辑 - 我想根据上面的示例,我可以在每次调用时对方法和类型名称进行硬编码。但我还是想知道是否有办法确定级别。
We're using Enterprise Library 4.1 for logging (and exception handling/cryptography).
Does anyone know a good way of determining the configured logging level at runtime? I've written a LogUtility class to to make the logging calls, and am calling it as per this example:
LogUtility.LogVerbose(
string.Format("Processing event {0}", currentEvent.EventIDImported),
MethodBase.GetCurrentMethod().Name,
this.GetType().Name
);
I understand that it won't actually get logged to file unless the logging level is set to an appropriate level, in app.config in my case. But I don't really want the method parameters, i.e. the method and type names, and in some cases the actual strings being logged, to be evaluated unless absolutely necessary.
Does this seem like a valid concern? Our app can have tens of millions of iterations and logging points. If possible I'd like to set a flag based on the configured log level, and check that before making the method call above.
EDIT - I guess in terms of the example above I could hard-code method and type names on every call. But I'd still like to know if there's a way of determining the level.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基于上述内容,我认为您应该看看
LogWriter
的ShouldLog
方法。它将让您确定是否将根据当前配置记录LogEntry
,并且您可以(希望)避免创建不需要的对象。从 Enterprise Library 4.1 借用代码 演练:在构造日志消息之前检查筛选器状态< /a>:
由于您使用自己的
LogUtility
类,您可能希望在LogUtility
上创建一个名为ShouldLogVerbose
或的静态属性IsVerboseEnabled
和该属性内部使用“正确”构造的LogEntry
(针对您的应用程序)来确定是否将记录消息。例如Based on the above I think you should take a look at the
ShouldLog
method ofLogWriter
. It will let you determine if aLogEntry
will be logged based on the current configuration and you can (hopefully) avoid creating objects that are not required.To borrow the code from the Enterprise Library 4.1 Walkthrough: Checking Filter Status Before Constructing Log Messages:
Since you are using your own
LogUtility
class you would probably want to create a static property onLogUtility
calledShouldLogVerbose
orIsVerboseEnabled
and inside of that property use a "properly" constructedLogEntry
(for your application) to determine if the message would be logged. e.g.对于给定类别,您可以执行以下操作:
请参阅 http://msdn.microsoft.com/en-us/library/microsoft.practices.enterpriselibrary.logging.logsource_members.aspx
For a given category, you can do the following:
See http://msdn.microsoft.com/en-us/library/microsoft.practices.enterpriselibrary.logging.logsource_members.aspx