Keyvan Nayyeri

God breathing through me

Health Monitoring in ASP.NET 2.0

This topic is covered by others before but this post by Rick pinged me to write about it to give an introduction for one of my incoming posts.

Rick as an administrator for a shared host, ASPnix, has disabled debugging on their servers but their clients got in trouble to debug their applications and are looking for a way.  Rick contacted Scott and he suggested to use ASP.NET 2.0 Health Monitoring feature then create a simple reporting system to let users be aware of their application errors.

I want to write a very simple report system for him so will produce an introduction to Health Monitoring in ASP.NET 2.0 in this post.

Who doesn't know?!  It's strongly recommended to disable debugging for your ASP.NET applications before deploying them to server but often developers forget it and this eats their server's resources.  When it comes to a shared host, problem gets bigger and some hosters disable debugging on their servers via machine.config.  But there are some errors that arise when an applications deploys on server and developers need to be aware of them.

On the other hand any professional site needs a mechanism to log its events/errors and lets its admin(s) to solve them.  This mechanism is provided in Community Server for ASP.NET 1.1 and 2.0 both and I talked about it here.  But ASP.NET 2.0 came with a new feature called Health Monitoring to assist developers and admins to easily set up it and log their application events.

ASP.NET 2.0 enables you to log your site events into SQL Server database, get them via email, log them in server's Event Logs, map them to Windows Management Instrumentation or write your own custom provider for this purpose.

Scott Allen had a post about this feature that you can read.

As same as Membership or Role Manager you can configure your Health Monitoring to use a provider and choose which kind of events should be logged via your Health Monitoring configurations.  For instance I create a simple website, use aspnet_regsql via command prompt to prepare my SQL Server database and put following code in my Web.config:

<healthMonitoring enabled="true">

  <providers>

    <add

      name="SqlEventProvider"

      connectionStringName="Health"

      maxEventDetailsLength="1000000"

      buffer="false"

      bufferMode="Notification"

      type="System.Web.Management.SqlWebEventProvider,

      System.Web,Version=2.0.0.0,Culture=neutral,

      PublicKeyToken=b03f5f7f11d50a3a" />

  </providers>

  <rules>

    <add name="AllEvents"

      eventName="All Events"

      provider="SqlEventProvider"/>

  </rules>

</healthMonitoring>

It uses Health connectionstring to connect to my database and log all events in my application.  Now I put a simple code like what follows in one of my web pages  to throw a new exception:

protected void Page_Load(object sender, EventArgs e)

{

    throw new ApplicationException

        ("This is an error thrown to be logged in database.");

}

After hitting this page I'll get an error.  Now I open my database and see the list of application events.  This error is logged there:

Logged Error

I could configure my Health Monitoring to only save application errors as well.   For more information about Health Monitoring in ASP.NET 2.0 refer to MSDN.

I'll write a simple reporting ASP.NET 2.0 website to list all errors for users.  More will come soon.

10 Comments

Jaxon Rice
Sep 14, 2006 5:51 AM
#
Nice work Keyvan. I have been looking for a solution to the ASPNIX debugging issue. I am lookin gofrward to see what you come up with

Sonu Kapoor
Sep 14, 2006 7:03 AM
#
This is a nice solution. In my 1.1 application, I used ELMAH. Take a look: http://www.gotdotnet.com/workspaces/workspace.aspx?id=f18bab11-162c-4267-a46e-72438c38df6f

Keyvan Nayyeri
Sep 14, 2006 7:26 AM
#
Yeah, I know about it. Still ELMAH is a great solution for 2.0 but Health Monitoring is more general because normal developers often are attracted with Microsoft's solutions (Scott Mitchell is known for us but not for all developers!). I think ELMAH doesn't have the functionality that is necessary to hosters to provide reports per user so writing same application is required for it.

Vikram
Sep 18, 2006 3:00 AM
#
Here is a detailed look at health monitoring system http://www.vikramlakhotia.com/Health_Monitoring_system_for_your_WEB_Application.aspx

Atif Aziz
Sep 21, 2006 1:13 AM
#
Keyvan, >> I think ELMAH doesn't have the functionality that is necessary to hosters to provide reports per user so writing same application is required for it. << Small correction. ELMAH supports this out of the box. It provides an error log view, RSS feed and mail per application, even with debugging turned off and custom errors turned on. You can deploy ELMAH in machine.config to enable it for all web applications, back it with a single database and yet provide per-application view without a single line of programming for 1.x or 2.0. In fact, what ELMAH won't allow is a single view for applications without code modification. The rationale was that one application shouldn't need to snoop around at another application's log.

Keyvan Nayyeri
Sep 21, 2006 1:38 AM
#
Thanks Atif for your correction, I didn't know about that because haven't worked in depth with ELMAH per server. And also thanks for your great HTTPModule + HTTPHandler :-)

Keyvan Nayyeri
Sep 22, 2006 7:44 AM
#
My first article on DotNetSlackers has been published: How to Write a Custom Web Event Provider for ASP.NET

Йордан Димитров
Sep 26, 2006 11:35 PM
#
Web Brennan’s Blog: Web Development Tools for the Power Developer Scott on Writing:Top Ten ASP.NET Tips...

Mehdi Mohammadi
Nov 05, 2006 10:53 PM
#
I have config health monitoring for my web application , i want to test that with " throw new ApplicationException ("This is an error thrown to be logged in database."); " but it did not log any thing please help what is the problem?

Keyvan Nayyeri
Jan 23, 2007 1:15 PM
#
Regular visitors can remember my post about Health Monitoring in ASP.NET 2.0 and my article about Writing

Leave a Comment





Ads Powered by Lake Quincy Media Network