2

我在 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 的设置?

4

4 回答 4

3

在 C# 中:

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

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

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

于 2010-01-05T00:56:54.387 回答
0

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

于 2009-11-09T05:12:08.597 回答
0

根据 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
于 2010-01-04T23:46:03.093 回答
0

您可以使用 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

于 2011-02-26T19:29:41.803 回答