In ASP.NET 2.0, events can be monitored using the healthMonitoring attribute. The healthMonitoring attribute is a method-based provider where you can construct your own provider. Using the healthMonitoring attribute, we can record errors, successful events, etc., for different data sources, such as event logs, Sql Server and even create our own providers by inheriting the WebEventProvider class. In this article, I'm going to walk through configuring a web application that detects SqlServer errors and sends an email to someone's email address. First, take a look at the healthMonitoring program fragment in web.config, where you can create the events you will use.
<healthMonitoring Enabled="true|false" heartBeatInterval="time interval">
<bufferModes>... </bufferModes>
<providers>... </providers>
<eventMappings>... </eventMappings>
<profiles>... </profiles>
<rules>... </rules>
</healthMonitoring>
If you look at the <healthMonitoring> element, you can determine whether you can set the attribute to be valid or invalid, and you can also specify the time interval for WebHeaderBeatEvent to be awakened. healthMonitoring has 5 children.
bufferModes, where you can define the buffer size of a Provider.
Providers, here describes the Providers that handle the event.
eventMappings, here you can draw event names related to friendly event types.
profiles, where you define a collection of parameter sets that can be used to configure events.
rules, draw the event graph of Providers here.
You can read more about these elements in the VS 2550 documentation.
Before continuing, here is a list of some of the Providers in ASP.NET:
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
does not need to explain these, the names tell us what they do. Also mention that SqlWebEventProvider relies on Sql server to work, which stores events in the aspnet_Web_Event table. In order to install this database, the aspnet_regsql.exe wizard located in the framework folder must be run.
Now, configure the program to have a login error for the Sql server provider and generate an error when sending an email.
The following is an example of using SqlWebEventProvider and 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 ..." [email protected] [email protected] 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 In , use the Sql provider to record all error events, and use the mail provider to send a message when the Web request error event is awakened.
Here are some events released 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
can use these events to profile a provider. You can also create your own events inherited from the WebBaseEvent class.
To automatically wake up an event, you can use the wakeup method of the WebBaseEvent class:
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();
}