views:

93

answers:

3

I have set up ASP.Net health monitoring in web.config:

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

And I am attempting to read the configuration of this provider in code:

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

[Note: not actual production code, hard-coded & condensed for brevity]
The problem: While the ElementInformation.Properties collection contains the keys as expected, the Value is "Nothing" and so are all other properties of "Properties("to")".
How can I access the settings of the Provider?

A: 

I've resorted to reading the web.config file as an XML document and using the nodes selected with the XPath: configuration/system.web/healthMonitoring/providers/*[@name]

AUSteve
A: 

Based on AUSteve's answer I used the below code.

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
Tim Santeford
+1  A: 

In C#:

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

section.Providers[3].Parameters["to"] returns "[email protected]".

(3 assumes that the providers list wasn't cleared in web.config.)

Nariman
I'm sure I tried the Parameters collection but it works now, cheers.
AUSteve