【问题标题】:Accessing HealthMonitoring Provider settings programmatically以编程方式访问 HealthMonitoring Provider 设置
【发布时间】:2010-12-13 16:08:10
【问题描述】:

我已经在 web.config 中设置了 ASP.Net 健康监控:

<healthMonitoring enabled="true">
<providers>
<clear />
<add name="CriticalMailEventProvider" type="System.Web.Management.SimpleMailWebEventProvider"
from="appErrors@myhost.com" to="me@myhost.com"
bodyHeader="Application Error!" bodyFooter="Please investigate ASAP." 
subjectPrefix="ERROR: " buffer="true" bufferMode="Critical Notification" 
maxEventLength="8192" maxMessagesPerNotification="1"
/>
</providers></healthMonitoring>

我正在尝试在代码中读取此提供程序的配置:

Dim HealthMonitoring As System.Web.Configuration.HealthMonitoringSection
Dim ToAddress As String
HealthMonitoring = CType(WebConfigurationManager.GetWebApplicationSection("system.web/healthMonitoring"), HealthMonitoringSection)
ToAddress = HealthMonitoring.Providers(0).ElementInformation.Properties("to").Value.ToString

[注意:不是实际的生产代码,为了简洁,硬编码和压缩]
问题:虽然 ElementInformation.Properties 集合包含预期的键,但值是“Nothing” "Properties("to")" 的所有其他属性也是如此。
如何访问 Provider 的设置?

【问题讨论】:

  • 任何解决方案,任何示例完整代码?

标签: asp.net configuration health-monitoring


【解决方案1】:

您可以使用 Initialize 方法访问您在 web.config 中指定的属性。只需访问 NameValueCollection(并在调用 base.Initialize 之前将其删除您的自定义属性)

本文展示了一个 C# 实现:http://www.tomot.de/en-us/article/6/asp.net/how-to-create-a-custom-healthmonitoring-provider-that-sends-e-mails

【讨论】:

    【解决方案2】:

    在 C# 中:

    HealthMonitoringSection section = WebConfigurationManager.GetWebApplicationSection("system.web/healthMonitoring") as HealthMonitoringSection;
    

    section.Providers[3].Parameters["to"] 返回 "me@myhost.com"

    (3 假设未在 web.config 中清除提供者列表。)

    【讨论】:

    • 我确定我尝试了参数集合,但它现在可以工作了,干杯。
    【解决方案3】:

    根据 AUsteve 的回答,我使用了以下代码。

    Dim xmldoc As New System.Xml.XmlDocument
    xmldoc.Load(HttpContext.Current.Server.MapPath("Web.config"))
    Dim xmlnsManager As System.Xml.XmlNamespaceManager = New System.Xml.XmlNamespaceManager(xmldoc.NameTable)
    xmlnsManager.AddNamespace("nc", "http://schemas.microsoft.com/.NetConfiguration/v2.0")
    
    Dim emailTo As String = xmldoc.SelectSingleNode("/configuration/system.web/healthMonitoring/providers/add", xmlnsManager).Attributes("to").Value.ToString
    

    【讨论】:

      【解决方案4】:

      我已将 web.config 文件作为 XML 文档读取并使用通过 XPath 选择的节点:configuration/system.web/healthMonitoring/providers/*[@name]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-11
        • 2015-05-04
        • 2020-12-13
        相关资源
        最近更新 更多