Introduction
Always monitoring a web application has been a common requirement for professional developers and site administrators. For ASP.NET 1.x there were some solutions that were written by community such as famous ELMAH (Error Logging Modules and Handlers) but now ASP.NET 2.0 has its own mechanism to let developers monitor their applications. There are some default providers to let you get events via email, log them into SQL Server database or server's Log Events or map them to WMI.
There is also another available option for you to write your own provider. In this article I show you how to do this by creating an XML provider step by step.
How to Write a Custom Provider
Writing a custom Health Monitoring web event provider is as easy as creating a Class Library project, a class in it and write a class that is inherited from System.Web.Management.BufferedWebEventProvider abstract base class and overriding its methods.
There are four methods in BufferedWebEventProvider abstract base class that you should override:
- Initialize(string, NameValueCollection): Occurs when your web event provider is being initialized.
- ProcessEvent(WebBaseEvent): Occurs when an event occurs in your application. If provider is configured to buffer events, they will be added to buffer otherwise you need to save them.
- ProcessEventFlush(WebEventBufferFlushInfo): Occurs when provider is being flushed and you need to implement your logic to save all buffered events.
- Shutdown(): Occurs when provider is being shut down.
User can configure this provider to buffer events so you need to consider this case in your code and try to save buffered events.
Write an XML Provider
Now I show you how to override those methods to write a custom web event provider. Before anything you need to know that I used a DataSet to save my event data into an XML file.
First I create a class, add necessary references and inherit it from BufferedWebEventProvider abstract base class.
I add some private variables to keep my data such as my XML file path, my provider name and my DataSet to save event data.
My implementation for Initialize() method tries to set some values for my private members then creates an initial empty XML file with correct structure and saves it by calling CreateInitialFile() private method.
Now I override ProcessEvent() and ProcessEventFlush() methods to add events to my DataSet based on user configuration for buffering events then save them into the XML file.
GetRow() and SaveData() private methods are very simple. First one creates a DataRow object from the properties of a WebBaseEvent object and second one saves the content of my DataSet into XML file.
Finally I don't need to override anything for ShutDown() method so simply call it from base class.
Test
Now that I have an XML web event provider, which can save my ASP.NET 2.0 application events into an XML file. First I create a simple ASP.NET 2.0 website that has only one page which contains nothing except raising an exception:
Now it's time to configure my application to use my XML web event provider.
By hitting my page I get an error. After this I can check my XML file to see the events logged there:
You can extend my provider to save more properties for events and write a reporting application to show this data.
Summary
In this article I talked about the process you need to follow to create a custom web event provider for ASP.NET 2.0 Health Monitoring feature. I showed this by implementing an XML web event provider and describing the process step by step.
Download
You can download the source code of my XML web event provider.
About Keyvan Nayyeri
 |
Keyvan is a software architect and developer who has a bachelor of science degree in applied mathematics. He was born in Kermanshah, Kurdistan, in 1984.
Keyvan’s main focus is on Microsoft development technologies and their related technologies such as mark-up languages. He’s also experie...
This author has published 4 articles on DotNetSlackers. View other articles or the complete profile here.
|
You might also be interested in the following related blog posts
How To: Silverlight grid hierarchy load on demand using MVVM and RIA services
read more
Health Monitoring and ASP.NET MVC
read more
Announcing the WebsiteSpark Program
read more
Designer Support for One-Way navigations in Entity Framework 4
read more
New content providers for the RadEditor and RadFileExplorer controls
read more
Migrated from Community Server to DasBlog
read more
Everything a small company needs to Build and Run your Web Apps from Microsoft for a Hundred Bucks !?
read more
StreamInsight
read more
Free software for you! WebsiteSpark let the mountain go to Microsoft instead.
read more
WebChart Drilldown Demo: A pathway to greater things
read more
|
|
Please login to rate or to leave a comment.