In ASP.Net 2.0 we can use the health monitoring feature to monitoring events. The health monitoring feature is a provider based solution, where we can build our own providers. With the health monitoring feature we can for example log errors, success events etc to different kind of data sources, such as, the event log, Sql Server and even to our own data source by creating our own provider by inheriting the WebEventProvider class. In this post I'm going to explain how we can configure our web application to monitoring errors to a Sql Server and also send events to someone’s email address. First lets take a look at health monitoring section in web.config, it’s where we can set up events and what providers we want to use etc.
<healthMonitoring
Enabled="true|false"
heartBeatInterval="time interval">
<bufferModes>... </bufferModes>
<providers>... </providers>
<eventMappings>... </eventMappings>
<profiles>... </profiles>
<rules>... </rules>
</healthMonitoring>
If we take a look at the <healtMonitoing> element, we can decide if we want to enable or disable the feature (default is false), we can also specify the beat interval for how of the WebHeartBeatEvent is raised (default is 0). The healthMonitoring has five children:
bufferModes, where we can define the buffering capabilities for a provider.
Providers, where we specify our providers that should process the event.
eventMappings, where we maps friendly event names to a related event type.
profiles, where we can define a collection of parameter sets that can be used when configuring events.
rules, where we maps events to providers.
You can read more about those elements in the documentation shipped with VS 2005.
Before we go on, here is a list of some of the providers that are in the box of ASP.Net 2.0:
System.Web.Management.MailWebEventProvider
System.Web.Management.SimpleMailWebEventProvider
System.Web.Management.TemplatedMailWebEventProvider
System.Web.Management.TraceWebEventProvider
System.Web.Management.EventLogWebEventProvider
System.Web.Management.SqlWebEventProvider
System.Web.Management.WmiWebEventProvider
I don’t think I need to explain those, the name will tell what they are used for. I can mention that the SqlWebEventProvider work against a Sql server database, it will save events to the aspnet_WebEvent_Events table. To install this data base, we must run the aspnet_regsql.exe wizard, located in the framework folder.
Now, let’s configure our application to log error events to the Sql server provider and also send an e-mail when something goes wrong.
Here is an example that uses the SqlWebEventProvider and the SimpleMailWebEventProvider to store error events:
<healthMonitoring enabled="true" heartBeatInterval="0">
<bufferModes>
<add name="Critical Notification"
maxBufferSize="100"
maxFlushSize="20"
urgentFlushThreshold="1"
regularFlushInterval="Infinite"
urgentFlushInterval="00:01:00"
maxBufferThreads="1"/>
<add name="Analysis"
maxBufferSize="1000"
maxFlushSize="100"
urgentFlushThreshold="100"
regularFlushInterval="00:05:00"
urgentFlushInterval="00:01:00"
maxBufferThreads="1"/>
</bufferModes>
<providers>
<add name="CriticalMailEventProvider"
type="System.Web.Management.SimpleMailWebEventProvider, System.Web ..."
from=info@nsquared2.net
to=fnormen@hotmail.com
priority="High"
bodyHeader="Warning!"
bodyFooter="Please investigate ASAP."
subjectPrefix="Action required."
buffer="true"
bufferMode="Critical Notification"
maxEventLength="4096"
maxSize="4096"
maxMessagesPerNotification="1"/>
<add name="SqlWebEventProvider"
type="System.Web.Management.SqlWebEventProvider, System.Web ..."
connectionStringName="LocalSqlServer"
maxEventDetailsLength="1073741823"
buffer="true"
bufferMode="Analysis"/>
</providers>
<eventMappings>
<add name="All Errors"
type="System.Web.Management.WebBaseErrorEvent, System.Web ..."/>
<add name="Request Processing Errors"
type="System.Web.Management.WebRequestErrorEvent, System.Web .../>
</eventMappings>
<profiles>
<add name="Default" minInstances="1" maxLimit="Infinite" minInterval="00:10:00"/>
</profiles>
<rules>
<add name="All Errors Default"
eventName="All Errors"
provider="SqlWebEventProvider"
profile="Default"
minInterval="00:00:30"/>
<add name="Request Processing Errors"
eventName="Request Processing Errors"
provider="CriticalMailEventProvider"
profile="Default"/>
</rules>
</healthMonitoring>
In this example, we will use the Sql provider to log all error events, and use the mail provider to send a message when a Web Request error event is raised.
Here are some of the events shipped with ASP.Net 2.0:
System.Web.Management.WebBaseEvent
System.Web.Management.WebHeartBeatEvent
System.Web.Management.WebApplicationLifetimeEvent
System.Web.Management.WebRequestEvent
System.Web.Management.WebBaseErrorEvent
System.Web.Management.WebErrorEvent
System.Web.Management.WebRequestErrorEvent
System.Web.Management.WebAuditEvent
System.Web.Management.WebFailureAuditEvent
System.Web.Management.WebSuccessAuditEvent
System.Web.Management.WebManagementEvent
System.Web.Management.WebViewStateFailureAuditEvent
System.Web.Management.WebAuthenticationFailureAuditEvent
System.Web.Management.WebAuthenticationSuccessAuditEvent
We can use those events to map against a provider. We can also create our own event by inheriting the WebBaseEvent class.
To raise an event programmatically we use the WebBaseEvent class’s Raise method:
try
{
//....
}
catch(Exception e)
{
if (HealthMonitoringManager.Enabled)
{
WebBaseEvent.Raise(new WebErrorEvent("My Error message", null, 5000, e));
}
}
or:
if (HealthMonitoringManager.Enabled)
{
WebErrorEvent event = new WebErrorEvent("My error message", null, 5000, e);
event.Raise();
}
If you have any questions, please ask me and I will try to answer them.