Home >Backend Development >C#.Net Tutorial >Tutorial on how to use the healthMonitor attribute in ASP.NET
In ASP.NET 2.0, you can use the healthMonitoring attribute to monitor events. 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 d8aea2075158d792937a399d255d2e1d 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 events.
EventMappings, here you can draw event names related to friendly event types.
Profiles, here define a collection of parameter sets that can be used to configure events.
Rules, draw the event diagram of Providers here.
You can read more about these elements in the VS 2550 documentation.
Before continuing, here is a list of some 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
There is no 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 ..." 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, the Sql provider is used to log all error events, and the mail provider is used 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
You can use these events to draw pictures of 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();}
The above is the detailed content of Tutorial on how to use the healthMonitor attribute in ASP.NET. For more information, please follow other related articles on the PHP Chinese website!