在运行时关闭 LogEnabled Filter - 日志记录企业应用程序块

发布于 2024-08-31 10:50:58 字数 274 浏览 8 评论 0原文

所以故事是这样的:

我正在使用微软提供的日志记录企业应用程序块来记录我们应用程序中的事件。

目标是在加载时启用事件查看器日志记录,然后将其关闭。

我所做的是添加 EventLog TraceListener,它将所有日志写入事件查看器。我想做的是在应用程序加载完成后禁用它。我能想到的唯一方法是添加 LogEnabled 过滤器,然后将其关闭。

但是,我不知道如何在运行时访问此过滤器并禁用此侦听器上的日志。

如果您有想法,请分享。

谢谢

so the story go like this:

i'm using the Logging Enterprise Application block that provided by microsoft to log events in our application.

The goal is to enabled event viewer logging at loading and then turn it off.

What i did is to add EventLog TraceListener which write all our logs into the event viewer. What i'm trying to do is to disable it after the application was done loading. The only way that i can thing about is to add a LogEnabled Filter and then turn it off.

However, i don't know how to access this filter at run time and disable log on this listener.

if you have an idea, please share.

Thanks

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

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

发布评论

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

评论(1

涙—继续流 2024-09-07 10:50:58

企业库日志记录提供了很多需要调整的地方。那么,在开始更改配置之前,也许一些开箱即用的设置可以让您做您想做的事情?

在我看来,您希望应用程序在启动期间记录一些消息,然后不再记录更多消息。我假设您明确知道何时完成记录启动消息。

如果上述情况成立,我认为最简单的方法是使用 LogEntry Priority 属性。

在配置文件中设置一个优先级过滤器,其最小优先级为 2:

<logFilters>
  <add minimumPriority="2" maximumPriority="2147483647" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.PriorityFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
    name="Priority" />
  <add enabled="true" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
    name="LogEnabled Filter" />
</logFilters>

然后在启动期间使用优先级 2(或更高)来记录消息。然后,当您知道已完成时,将优先级设置为 1。

我会更好地封装它,但代码示例如下所示:

public class MyApp
{
    public static int LogPriority = 2;
    public static readonly string MyAppCategory = "MyAppCategory";

    static void Main()
    {
        Logger.Write("Loading 1...", MyAppCategory, LogPriority);
        // ...       
        Logger.Write("Loading 2...", MyAppCategory, LogPriority);

        // Done loading so turn off logging
        LogPriority = 1;

        Logger.Write("Message not logged", MyAppCategory, LogPriority);
    }
}

现在,如果您想为应用程序的其余部分打开日志记录,只需将优先级过滤器从 2 降低到 1,所有内容都会被记录。

Enterprise Library logging offers a bunch of bits to twiddle. So, before getting into changing the configuration maybe some of the out of the box settings can let you do what you want?

It sounds to me like you want your application to log some messages during startup and then not to log any more messages. I'm assuming that you explicitly know when you are done logging startup messages.

If the above is true I think the easiest way would be to use the LogEntry Priority property.

In the configuration file setup a Priority Filter with a minimum priority of 2:

<logFilters>
  <add minimumPriority="2" maximumPriority="2147483647" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.PriorityFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
    name="Priority" />
  <add enabled="true" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
    name="LogEnabled Filter" />
</logFilters>

Then during startup use a priority of 2 (or greater) to log your messages. Then when you know you are done set the priority down to 1.

I would encapsulate it better but a code sample would look like:

public class MyApp
{
    public static int LogPriority = 2;
    public static readonly string MyAppCategory = "MyAppCategory";

    static void Main()
    {
        Logger.Write("Loading 1...", MyAppCategory, LogPriority);
        // ...       
        Logger.Write("Loading 2...", MyAppCategory, LogPriority);

        // Done loading so turn off logging
        LogPriority = 1;

        Logger.Write("Message not logged", MyAppCategory, LogPriority);
    }
}

Now if you ever want to turn on logging for the rest of your app, just lower the priority filter from 2 to 1 and everything will get logged.

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