如何编写和配置 ASP.Net Healthmonitoring 的自定义提供程序?

发布于 2024-10-10 07:32:12 字数 2215 浏览 0 评论 0原文

我正在寻找有关如何创建和使用 ASP.Net Healthmonitoring 的自定义提供程序的演练。

到目前为止,我只与在错误时生成电子邮件的电子邮件提供商合作。基本上我想做同样的事情,但更灵活:
我想以一种允许我访问事件的方式使用 HealthMonitoring 功能(我不想使用 global.asax 中的 Application_OnError 事件),该事件会像“OnNewHealthMonitoringEntry”一样抛出,其中包含电子邮件,运行自定义代码。

编辑:
基于此处提供的源代码 http://www.asp.net/general/videos/how-do-i-create-a-custom-provider-for-logging-health-monitoring-events 我能够构建我自己的自定义提供程序并实施它。现在我想添加一些新属性来配置我的自定义提供程序。 web.config 如下所示:

<healthMonitoring>
    <bufferModes>
        <add name="Log Notification" maxBufferSize="1" maxFlushSize="1" urgentFlushThreshold="1" regularFlushInterval="Infinite" urgentFlushInterval="00:00:10"/>
    </bufferModes>
    <providers>
        <add name="FileEventProvider" buffer="true" bufferMode="Log Notification" type="healthmonitoringtest.FileHealthMonitorEventProvider"/>
    </providers>
    <profiles>
        <add name="Custom" minInstances="1" maxLimit="Infinite" minInterval="00:00:00"/>
    </profiles>
    <rules>
        <add name="File Event Provider" eventName="All Errors" provider="FileEventProvider" profile="Custom"/>
    </rules>
</healthMonitoring>

如果我尝试向提供程序添加属性,如下所示,

<providers>
    <add name="FileEventProvider" buffer="true" bufferMode="Log Notification" foo="bar"  type="healthmonitoringtest.FileHealthMonitorEventProvider"/>
</providers>

我会收到一条错误消息:

类型异常 '系统.配置.ConfigurationErrorsException' 发生在 System.Web.dll 中但不是 在用户代码中处理附加 信息:意外的属性 foo 在配置中 文件事件提供者。

是否可以将自定义提供程序所需的配置存储在 healthMonitoring 部分附近?我想我可以将设置包含到 appSettings 节点中,但我想使用属性(在 healthMonitoring 节点内)以某种方式配置它。这可能吗?

编辑2: 您可以看看这篇文章: http://www.tomot.de/en-us/article/6/asp.net/how-to-create-a-custom-healthmonitoring-provider-发送电子邮件

I'm looking for a walkthrough on how to create and use a custom provider for ASP.Net Healthmonitoring.

So far I've only worked with the e-mail provider that generates e-mails on errors. Basically I want to do the same, but with more flexibility:
I want to use the HealthMonitoring features (I don't want to use the Application_OnError event in the global.asax) in a way that allows me have access to an event, that gets thrown like "OnNewHealthMonitoringEntry" with all the information provided in the e-mail, to run custom code.

Edit:
Based on the source code provided here http://www.asp.net/general/videos/how-do-i-create-a-custom-provider-for-logging-health-monitoring-events I was able to build my own custom provider and implement it. Now I want to add some new attributes to configure my custom provider.
Here is what the web.config looks like:

<healthMonitoring>
    <bufferModes>
        <add name="Log Notification" maxBufferSize="1" maxFlushSize="1" urgentFlushThreshold="1" regularFlushInterval="Infinite" urgentFlushInterval="00:00:10"/>
    </bufferModes>
    <providers>
        <add name="FileEventProvider" buffer="true" bufferMode="Log Notification" type="healthmonitoringtest.FileHealthMonitorEventProvider"/>
    </providers>
    <profiles>
        <add name="Custom" minInstances="1" maxLimit="Infinite" minInterval="00:00:00"/>
    </profiles>
    <rules>
        <add name="File Event Provider" eventName="All Errors" provider="FileEventProvider" profile="Custom"/>
    </rules>
</healthMonitoring>

If I attempt to add an attribute to the provider, like this

<providers>
    <add name="FileEventProvider" buffer="true" bufferMode="Log Notification" foo="bar"  type="healthmonitoringtest.FileHealthMonitorEventProvider"/>
</providers>

I'll get an error saying:

An exception of type
'System.Configuration.ConfigurationErrorsException'
occurred in System.Web.dll but was not
handled in user code Additional
information: Unexpected attribute foo
in the configuration of the
FileEventProvider.

Is it possible to store configuration necessary for custom provider close to the healthMonitoring section? I guess I could include the settings into the appSettings node, but I'd like to configure it somehow with attributes (inside the healthMonitoring node). Is that possible?

Edit2:
You might take a look at this article: http://www.tomot.de/en-us/article/6/asp.net/how-to-create-a-custom-healthmonitoring-provider-that-sends-e-mails

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

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

发布评论

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

评论(1

随风而去 2024-10-17 07:32:12

以下系列文章 将引导您了解使用运行状况监控系统直至创建自定义事件的基础知识。

然后以下 26 分钟的视频将引导您创建一个自定义提供程序,将事件记录到基于文本的日志文件中。

基于评论的更新

查看您的更新并使用 Reflector 查看您的自定义提供程序所基于的 BufferedWebEventProvider 类的源代码,我发现 Initialize 方法BufferedWebEventProvider 在最后进行检查,看看是否有任何它无法识别的属性。这是通过在将值分配给 BufferedWebEventProvider 的属性或字段后立即从配置 NameValueCollection 参数中删除值来完成的。然后检查 config 参数是否为空,如果不为空则意味着添加了额外的属性,这会导致抛出异常。

至于如何解决此问题,一种选择是:

  1. 将对 base.Initialize 的调用移至方法末尾,
  2. 就像提供程序一样将附加属性分配给变量后立即将其删除。

像下面这样的东西会起作用:

public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
    {
        foo = config["foo"];
        if (String.IsNullOrEmpty(foo))
        {
            // You can set a default value for foo
        }

        //remove foo from the config just like BufferedWebEventProvider with the other
        //attributes. Note that it doesn't matter if someone didn't proivde a foo attribute
        //because the NameValueCollection remains unchanged if you call its Remove method
        //and the name doesn't exist.
        config.Remove("foo");

        base.Initialize(name, config);
    }

希望这对你有用。

The following series of articles will take you through the basics of using the Health Monitoring System upto creating Custom Events.

Then the following 26 minute video will take you through creating a custom provider that records events to a text-based log file.

UPDATE Based on Comment

Looking at your update and using Reflector to look at the source for the BufferedWebEventProvider class that you base your custom provider on, I have found that the Initialize method in BufferedWebEventProvider does a check at the end to see if there are any attributes that it doesn't recognize. This is done by removing values from the config NameValueCollection parameter as soon as they are assigned to the properties or fields of the BufferedWebEventProvider. Then a check is done to see if the config parameter is empty and if not that means that there are extra attributes added, which causes an exception to be thrown.

As to how to fix this problem, one option is to:

  1. Move the call to base.Initialize to the end of the method
  2. Remove the additional attributes as soon as you assign them to variables just like the provider does.

Something like the following would work:

public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
    {
        foo = config["foo"];
        if (String.IsNullOrEmpty(foo))
        {
            // You can set a default value for foo
        }

        //remove foo from the config just like BufferedWebEventProvider with the other
        //attributes. Note that it doesn't matter if someone didn't proivde a foo attribute
        //because the NameValueCollection remains unchanged if you call its Remove method
        //and the name doesn't exist.
        config.Remove("foo");

        base.Initialize(name, config);
    }

Hopefully this works out for you.

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