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>

我会收到一条错误消息:

System.Web.dll 中发生“System.Configuration.ConfigurationErrorsException”类型的异常,但未在用户代码中处理附加信息:FileEventProvider 配置中的意外属性 foo。

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

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

4

1 回答 1

2

以下系列文章将带您了解使用健康监控系统的基础知识,直至创建自定义事件。

接下来的26 分钟视频将带您了解如何创建自定义提供程序,将事件记录到基于文本的日志文件中。

更新基于评论

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

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

  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);
    }

希望这对你有用。

于 2011-01-04T18:31:18.187 回答