Published: 22 Sep 2006
By: Keyvan Nayyeri

In this article Keyvan shows the process to write a custom web event provider for new Health Monitoring feature in ASP.NET 2.0 and creates a custom XML provider as a sample.

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.

Other articles in this category


Inversion of Control and Dependency Injection with Castle Windsor Container - Part I
Inversion of Control (IoC) and Dependency Injection (DI) are two related practices in software devel...
How to Write a Provider Model
In this article Keyvan teaches you how to write your own data provider for .NET applications using t...
Inversion of Control and Dependency Injection with Castle Windsor Container - Part II
Since the previous article didn't show much of the features offered by Castle Windsor Container, I w...
Inversion of Control and Dependency Injection with Castle Windsor Container - Part IV
In this article I introduce the missing core features I didn't tackle before.
Inversion of Control and Dependency Injection with Castle Windsor Container - Part III
In the previous article I showed how to take advantage of some of the features offered by Windsor Co...

You might also be interested in the following related blog posts


Announcing Microsoft Ajax Library (Preview 6) and the Microsoft Ajax Minifier read more
Announcing the Microsoft AJAX CDN read more
Announcing the WebsiteSpark Program read more
Everything a small company needs to Build and Run your Web Apps from Microsoft for a Hundred Bucks !? read more
Dovetail is Hiring a Junior-to-Mid-level .NET Developer read more
Health Monitoring and ASP.NET MVC read more
Accessing Images On Flickr From An ASP.NET Website Using The Flickr.Net Library read more
Auto-Start ASP.NET Applications (VS 2010 and .NET 4.0 Series) 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
Top
 
 
 

Please login to rate or to leave a comment.

Product Spotlight