Health monitoring in ASP.NET 2.0

Apr 24, 2006

The new ASP.NET 2.0 provider model has once again amazed me. It lets you write to the EventLog, Sql Server and WMI as standard and lets you write your own provider that monitors the different events that occurs in a web application or ones you raise yourself. Read more on MSDN about using health monitoring in ASP.NET 2.0.

It also lets you send an email when an error occurs with writing anything but some lines in the web.config. This is amazing. I don’t know how many times I’ve written some code that would send me a mail when a critical or unexpected error happens. For smaller web applications, this is absolutely amazing, because you probably don’t want a big event logging framework when building a simple presentational website.

It is built so clever, that you don’t get an email every time an error occurs. You can set the interval or get with the one minute default. If five errors occur within that one minute, only one mail is sent with the information of all the errors so you mailbox won’t get flooded.

I couldn't find an example of how to use health monitoring to send an e-mail, but a lot of articles on the web led me in the right direction. After doing some research and trial-and-error I finally made it work.

Here is a very simple web.config file that sends an e-mail whenever an unhandled error occurs. Pay attention to the <healthMonitoring> and <system.net> section. These are the important ones that makes it work.

<?xml version="1.0"?>
<configuration>
   <appSettings/>
   <connectionStrings/>

   <system.web>
      <compilation debug="false" />
      <trace enabled="true" localOnly="false" />

      <healthMonitoring enabled="true">
         <providers>
            <add name="EmailProvider" 
               type="System.Web.Management.SimpleMailWebEventProvider"
               from="you@domain.com"
               to="you@domain.com"
               subjectPrefix="Error: "
               buffer="true"
               bufferMode="Notification" />
         </providers>
         <rules>
            <add provider="EmailProvider" name="All App Events" eventName="All Errors" />
         </rules>
      </healthMonitoring>

   </system.web>
   <system.net>
      <mailSettings>
         <smtp from="you@domain.com">
            <network host="smtp.domain.com" />
         </smtp>
      </mailSettings>
   </system.net>
</configuration>

How easy can it get? Truly amazing.

* $4.95/month BlogEngine.net Hosting – Click Here!

Comments (7) -

 lb
lb
7/26/2006 12:30:43 AM #

thanks Mads...
i just wish i'd found this sooner!
very nice write up.
lb

jonah
jonah United States
8/9/2007 5:24:05 AM #

Your links do not work in your RSS feed. They are relative...

Dan Atkinson
Dan Atkinson United Kingdom
8/9/2007 8:30:48 AM #

This baby saved my bacon this morning. How co-incidental that you mentioned it in your post!

Josh Stodola
Josh Stodola United States
8/9/2007 1:46:12 PM #

Amazing indeed, I wish I would have known about this along time ago.  Thanks for sharing, Mads!

TweeZz
TweeZz
10/4/2007 6:22:31 AM #

For people who want to implement it, here are a few more references I used:
aspnetresources.com/.../..._health_monitoring.aspx
msdn2.microsoft.com/en-us/library/ms998306.aspx
all default event mappings: msdn2.microsoft.com/en-us/library/yc5yk01w.aspx

e
e United States
12/19/2007 9:47:20 AM #

wow! this is just too easy!

soon we all will be out of jobs!

Dale Sides
Dale Sides United States
11/22/2008 1:40:52 AM #

You mentioned setting an interval in your article but I don't see it in the example, could you post an example on how to implement it? Thanks again, I can remove a lot of code from some of my sites.

Pingbacks and trackbacks (7)+

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.