企业库日志记录 - 如何在运行时获取配置的日志记录级别?

发布于 2024-11-08 04:24:29 字数 585 浏览 0 评论 0原文

我们使用 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 技术交流群。

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

发布评论

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

评论(2

暮年 2024-11-15 04:24:30

我真的不想要这个方法
参数,即方法和类型
名称,在某些情况下是实际的
正在记录、待评估的字符串
除非绝对必要。

基于上述内容,我认为您应该看看LogWriterShouldLog 方法。它将让您确定是否将根据当前配置记录 LogEntry,并且您可以(希望)避免创建不需要的对象。

从 Enterprise Library 4.1 借用代码 演练:在构造日志消息之前检查筛选器状态< /a>:

LogEntry logEntry = new LogEntry();
logEntry.Priority = 2;
logEntry.Categories.Add("Trace");
logEntry.Categories.Add("UI Events");

if (Logger.ShouldLog(logEntry))
{
  // Perform operations (possibly expensive) to gather additional information 
  // for the event to be logged. 
}
else
{
  // Event will not be logged. Your application can avoid the performance
  // penalty of collecting information for an event that will not be
  // logged.
}

由于您使用自己的 LogUtility 类,您可能希望在 LogUtility 上创建一个名为 ShouldLogVerbose的静态属性IsVerboseEnabled 和该属性内部使用“正确”构造的 LogEntry (针对您的应用程序)来确定是否将记录消息。例如

if (LogUtility.IsVerboseEnabled)
{
    LogUtility.LogVerbose(
        string.Format("Processing event {0}", currentEvent.EventIDImported), 
        MethodBase.GetCurrentMethod().Name, 
        this.GetType().Name
    );
}

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.

Based on the above I think you should take a look at the ShouldLog method of LogWriter. It will let you determine if a LogEntry 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:

LogEntry logEntry = new LogEntry();
logEntry.Priority = 2;
logEntry.Categories.Add("Trace");
logEntry.Categories.Add("UI Events");

if (Logger.ShouldLog(logEntry))
{
  // Perform operations (possibly expensive) to gather additional information 
  // for the event to be logged. 
}
else
{
  // Event will not be logged. Your application can avoid the performance
  // penalty of collecting information for an event that will not be
  // logged.
}

Since you are using your own LogUtility class you would probably want to create a static property on LogUtility called ShouldLogVerbose or IsVerboseEnabled and inside of that property use a "properly" constructed LogEntry (for your application) to determine if the message would be logged. e.g.

if (LogUtility.IsVerboseEnabled)
{
    LogUtility.LogVerbose(
        string.Format("Processing event {0}", currentEvent.EventIDImported), 
        MethodBase.GetCurrentMethod().Name, 
        this.GetType().Name
    );
}
伪装你 2024-11-15 04:24:30

对于给定类别,您可以执行以下操作:

logWriter.TraceSources["category"].Level

请参阅 http://msdn.microsoft.com/en-us/library/microsoft.practices.enterpriselibrary.logging.logsource_members.aspx

For a given category, you can do the following:

logWriter.TraceSources["category"].Level

See http://msdn.microsoft.com/en-us/library/microsoft.practices.enterpriselibrary.logging.logsource_members.aspx

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