OdeToCode IC Logo

Health Monitoring and ASP.NET MVC

Monday, October 12, 2009

Health monitoring has been available in ASP.NET since v 2.0, and the health monitoring features can give you information you can’t find anywhere else. I was troubleshooting an MVC application recently and needed information about the application:

  • When was the application starting?
  • When was the application shutting down?
  • Why was the application shutting down?
  • When was view compilation taking place?

This is the type of information you can record with health monitoring in both Web Forms and MVC applications.

You’ll typically want to record health data into SQL Server for analysis. The first step is to create the ASP.NET “application services” database. You might already be using the database if you are using ASP.NET features like the SQL membership provider for forums authentication. If not, the aspnet_regsql tool will create the tables and procedures required for health monitoring in any database you choose.

Once the database tables are in place, you just need a bit of configuration in web.config. The following configuration is telling the health monitoring system to send application lifecycle events to a SQL Server provider.

      <add name="appDB" connectionString="data source=..." />
<healthMonitoring enabled="true">
    <add name="sqlProvider" 
         bufferMode="Notification" />

    <add name="lifeCycle"              
         eventName="Application Lifetime Events" />      

The health monitoring system will start to populate the aspnet_WebEvent_Events table with detailed event information. There is no built-in feature to display the events, but you can always setup a simple display view, connect with Microsoft Excel, or connect with any SQL query tool. The information is fairly detailed.

What about ELMAH?

Many people are using ELMAH for error logging. ELMAH is easier to use and configure compared to the built-in health monitoring features. ELMAH also includes a number of display options, including the ability to generate RSS feeds from logged events, and sending log events to Twitter. Hanselman has a great post on using ELMAH in an MVC application. ELMAH’s creator is Atif Aziz, who has a number of other interesting projects listed on his web site: www.raboof.com.

If you are already using ELMAH, you still might consider looking at health monitoring for some scenarios. ELMAH is a fantastic error reporting tool, but some of the information available from health monitoring is only available via health monitoring, since it has a close tie to the ASP.NET runtime. If you want to take the health monitoring events and publish them via ELMAH, than Eric Schoenholzer (@bluesware) has written an ElmahEventProvider you can download from the Files section of the ELMAH project site.