HealthMonitor attribute usage in ASP. NET

Source: Internet
Author: User

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. With the healthMonitoring attribute, we can record errors and successful events, and create our providers for different data sources, such as Event Logs, SQL Server, and even for ourselves by inheriting the WebEventProvider class. In this article, I plan to introduce a web application that monitors errors and sends emails to someone's email address. First, let's take a look at the healthMonitoring program fragment in web. config, where we can create the events to be used.

<HealthMonitoring Enabled = "true | false" heartBeatInterval = "time interval">
<BufferModes>... </bufferModes>
<Providers>... </providers>
<EventMappings>... </eventMappings>
<Profiles>... </profiles>
<Rules>... </rules>
</HealthMonitoring>

If you look at the

BufferModes, where the buffer size of a Provider can be defined.

Providers, indicating the Providers that process the event.

EventMappings, where you can draw event names related to the event type.

Profiles, which defines a set of parameters that can be used to configure events.

Rules, where the event map of Providers is drawn.

Read more about these elements in VS 2550.

Before proceeding, here is a list of Providers that contain 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 this. The names tell us what they do. SqlWebEventProvider relies on SQL server to store events in the aspnet_Web_Event table. To install the data library, run the aspnet_regsql.exe wizard in the frameworkfolder.

Now, configure a program that has a logon error message for the SQL server provider and sends an email to generate an error.

The following is an example of saving an error message event using SqlWebEventProvider and SimpleMailWebEventProvider.

<HealthMonitoring enabled = "true" heartBeatInterval = "0">
<BufferModes>
<Add name = "Critical Notification" maxBufferSize = "100" maxFlushSize = "20" inline = "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, SQL provider is used to record all the error events, and mail provider is used to send a message when the Web Request error event is awakened.

Here are some events released together 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 a picture of a provider. You can also create your own events inherited by the WebBaseEvent class.

To automatically wake up an event, you can use the WebBaseEvent class wake-up 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 ();
}
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.