Web events in ASP. NET 2.0

Source: Internet
Author: User
Tags time and date
ASP. NET 2.0 also provides full-featured application monitoring and health monitoring. This system is composed of a fully scalable event model and an event engine that can send events to multiple receivers. For example, you can configure your ASP. NET application to send an email every day, indicating that the server is running and the number of available memory is included. Similarly, you can create a link to a health event that does not handle exceptions. Exception content, request title, and time and date can all be sent to an error log record database.

ASP. NET 2.0 contains built-in events, including heartbeat, application Lifetime Events (start/stop/compile), and error trap events (unhandled exceptions ). However, you can easily build on these base classes to recreate the application and trigger your own events. For example, you may create a custom event to record when 100th users click a specific link.

ASP. NET 2.0 the real powerful function of the health monitoring system is that it is completely configurable through the Web. config and machine. config files. With Normal XML, you can define events, providers (event receivers), and send specific events to specific providers.

Create event

The event structure is similar to the exception. That is to say, apart from serving as a message container,EventThe class itself has almost no function. In terms of health monitoring, all eventsSytstem. Web. Management. webbaseeventInheritance. However, you can also derive from an advanced base class for special purposes (such as collecting HTTP request data or handling exceptions.

List 4. Custom events

using System;using System.Web.Management;public class CustomEvent : WebBaseEvent{  public const int EventCode = WebEventCodes.WebExtendedBase + 10;  public MyEvent(string message, object eventSource)                 : base(message, eventSource, EventCode)  { }}

The most important part of creating a custom event is to provide a unique eventcode. All built-in event codes are inWebeventcodesEnumeration. Custom events should haveWebeventcodes. webextendedbase+ The number starting from 1. In addition, the only common task in creating a custom event is to correctly initialize the event.

Use event

Although built-in events are automatically triggered, you can add the code to the application to start custom events at any time.

List 5. Raise an event

<script runat="server">    void Page_Load(Object sender, EventArgs e)    { // Raise a custom event MyEvent myEvent =           new MyEvent("loading webevent sample page", this); myEvent.Raise(); } </script> 

When you raise an event from the ASP. NET page, you only create a new instance of the event and then executeRaise ()Method.Raise ()Method to automatically pass the event instance to the health monitoring engine. The engine then maps the event to the configuration file and the provider, and hand over the event to the correct provider. The provider finally passes the event to the correct receiver.

Configure Health Monitoring

Health Monitoring is configured in a new region in the machine. config or web. config file. You can configure a web detection signal that periodically reports the application status. You can also configure an application to generate an event and pass it through various providers.

Configure events

Each event must be identified in the event ing area. Events are identified by unique names and complete types. Event names are used as links in the rule area.

LIST 6. Event ing area

<!-- Event mappings define the events that are monitored --> <eventMappings>      <add name="SampleWebRequests"           type="Samples.AspNet.SampleWebRequestEvent, SampleWebRequestEvent,                Version=0.0.0.0, Culture=neutral, PublicKeyToken=f0c63b9a560d5e5a"/> </eventMappings> 

ASP. NET 2.0 comes with several built-in events and is configured with the following names:

All events. All event names capture anyWebbaseevent. This event category is a broad catch-all for each event captured by the health monitoring system.

Detection Signal. Detect signal event usageWebheartbeateventProvides regular notifications about the web application status.

Application Lifetime Events. Application Lifetime Events include starting and stopping the application and recompiling part or whole of the application. These events are based onWebapplicationlifetimeeventAndLifetimeWhen an event occurs, the report date, time, and current status are displayed.

All Errors. All error categories collect any exceptions or errors detected by the system. These events are based onWebbaseerrorevent.

Infrastructure Error. UseWeberroreventCapture errors related to ASP. NET Runtime library or IIS. These events are subclasses of all error categories and are mainly related to system administrators rather than application developers.

Request processing error. Any errors or exceptions that occur during the request will triggerWebrequesterrorevent. This event records the incoming request and the errors associated with processing the request. Request Processing errors are also a subset of all error categories.

All reviews. You can use the health monitoring systemWebauditeventProvide review attempts. This event automatically records the operations of active users in the Web application. If you are using a simulation, review events will help you track who is using your application and how they are using persistence.

Failed Review.WebfailureauditeventIs a special audit event type. when a user attempts to log on to your web site with an invalid user name or password, it will be triggered. This event also occurs when a specified resource user cannot be verified.

Reviewed.WebsuccessauditeventIs the correspondence of failure events, as long as the user has verified or executed some other operations that require review records, it will happen.

Custom events are easy to create, although you must add code to your application to raise events.

Configure the provider

Each provider must be registered in the configuration file. Registering a provider requires a unique name and type of the application. This type contains the actualProviderThe complete strong name (strong name) of the class ). The provider name is used as a link in the rule area.

List 7. Providers

ASP. NET 2.0 comes with the WMI, Windows event monitor, SMTP email, and SQL Server database providers. By extending the correct base class, you can create custom providers to connect to other event receivers.

MAP events to providers

The last step to configure health monitoring is to use rules to connect events to the provider. The rule provides a link between a good event name, event class, provider, and event configuration file or category. You can also use rules to define a minimum interval for starting a specific event.

List 8. Rules

<!-- Rules link events to providers and profiles, and define intervals for event checking --> <rules>     <add name="Custom Database Events" eventName="CustomDBEvents"          provider="WmiEventProvider" profile="Database" minInterval="00:01:00" />     <add name="Standard Web Requests" eventName="All Events"          provider="SqlEventProvider" profile="Default" minInterval="00:01:00" /> </rules> 

The rule executes several different tasks configured by different attributes:

Name. The rule name is a friendly name that appears when the event is sent to the receiver.

Eventname.EventnameMaps to an event configured in the region.

Provider. A provider is a link to the provider configured in the region. Any event that matches this rule will be passed through this provider to the target supported by the provider. For example,System. Web. Management. sqlwebeventproviderThe event is automatically written to the SQL Server database.

Profile. Different providers use the configuration file attributes as filters for displaying events. For example, the email provider may immediately send a message for any event with an "urgent" configuration file, however, it is possible that only routine emails containing the daily "routine" configuration file events will be sent.

Mininterval. Some events, such as detection signals, must be triggered within the shortest interval. You can use this attribute to set the minimum event interval.

In this configuration area, you can set various health events and MAP events to various providers. For example, you may set a detection signal to send a WMI event every 10 minutes. Similarly, you can set an email alarm for any exceptions that are not captured.

Related Articles:
Http://support.microsoft.com/default.aspx? SCID = kb % 3bzh-cn % 3b893664
Http://blog.csdn.net/dshpsky/archive/2006/06/18/810893.aspx

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.