我在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
[注意:不是实际的生产代码,硬编码&amp;为简洁而浓缩]
问题:虽然ElementInformation.Properties集合包含预期的键,但Value是“Nothing”,所以“Properties(”to“)”的所有其他属性也是如此。
如何访问提供商的设置?
答案 0 :(得分:3)
在C#中:
HealthMonitoringSection section = WebConfigurationManager.GetWebApplicationSection("system.web/healthMonitoring") as HealthMonitoringSection;
section.Providers [3] .Parameters [“to”] 返回“me@myhost.com”。
(3假设在web.config中没有清除提供者列表。)
答案 1 :(得分:0)
我已经将web.config文件作为XML文档阅读,并使用XPath选择的节点:configuration / system.web / healthMonitoring / providers / * [@ name]
答案 2 :(得分: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
答案 3 :(得分:0)
您可以使用Initialize方法访问您在web.config中指定的属性。只需访问NameValueCollection(并在调用base.Initialize之前删除它的自定义属性)