In ASP.NET 2.0 we have a new powerful monitoring feature called “Web Events” which help keep track of important events happening during your application’s lifetime, such as the application is starting, is stopping, it has been recompiled, exceptions etc… Web events are consumed by providers (listeners), which read the information packaged with the event and then record the information. Each provider is written to record the Web event data in a different way. For example, one provider might write to the Windows event log, while another might send an e-mail message.
We have several built-in Web Events and 3 Providers available, and of course you can build your own using the classes provided in the System.Web.Management namespace; however there are only two events enabled in the rules element: All Errors and Failure Audits. Both are enabled and are subscribed to by the EventLogProvider.
In it’s simplest form, for example, you can tell ASP.NET to make an event log entry each time an application starts or shuts down by adding this to the <system.web> section of the web.config file for the application:
<healthMonitoring enabled="true" heartbeatInterval="0"> <rules> <add name="Lifetime event logging rule" eventName="Application Lifetime Events" provider="EventLogProvider" /> </rules> </healthMonitoring>
The sample code above creates new entries in the Application event log, but you can also choose to store them in Sql Server, or use the WMI provider, or send them by email etc…
Of course this has a performance cost: you can limit the number of event notifications that occur in a given time span and specify the interval between events using attributes of the rules element. The following example shows a fragment from the configuration file in which the Default profile is configured for Failure Audit events. The Default profile specifies the following settings
- The minimum number of times an event can occur before an event notification is sent is 1
- The maximum number of times an event can occur before notifications stop is 2147483647 (infinite)
- The minimum time interval between two events is one minute
By default, the ASP.NET health-monitoring system can deliver Web event data using the built-in providers (EventLogWebEventProvider, SqlWebEventProvider, WmiWebEventProvider, SimpleMailWebEventProvider, TemplateMailWebEventProvider and TraceWebEventProvider); more than one provider can listen for the same event, and more than one event can be consumed by the same provider.
Back to the sample code above, I used it in a case where a customer reported some unexpected AppDomain restarts; after enabling Web Events for the target application we got several entries in the Application event log similar to the following:
Event Type: Information
Event Source: ASP.NET 2.0.50727.0
Event Category: Web Event
Event ID: 1305
Date: 10/03/2007
Time: 10.53.47
User: N/A
Computer: CARLOC02
Description:
Event code: 1002
Event message: Application is shutting down. Reason: Configuration changed.
Event time: 10/03/2007 10.53.47
Event time (UTC): 10/03/2007 9.53.47
Event ID: 62cf5eed0f54464396b8f4d84e966a43
Event sequence: 12
Event occurrence: 1
Event detail code: 50004
Application information:
Application domain: 85c63a25-1-128179939913281250
Trust level: Full
Application Virtual Path: /WebEventsTest
Application Path: C:\Carlo\Progetti\VS2005\WebEventsTest\
Machine name: CARLOC02
Quite clear, isn’t it? It turned out that their antivirus was “touching” their web.config and this triggered a change notification the runtime interpreted as if someone had modified the application configuration file.
The above is just a sample, if you want to know more and take advantage of this new cool feature have a look at ASP.NET health monitoring overview, Web Events in ASP.NET 2.0, Quick Start Tutorials: Web Events.
Cheers