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.

<system.web>
  <connectionStrings>
      <add name="appDB" connectionString="data source=..." />
  </connectionStrings>
<system.web>
<healthMonitoring enabled="true">
  
  <providers>
    <add name="sqlProvider" 
         type="System.Web.Management.SqlWebEventProvider"
         connectionStringName="appDB"
         buffer="false" 
         bufferMode="Notification" />
  </providers>

  <rules>
    <add name="lifeCycle"              
         provider="sqlProvider"
         eventName="Application Lifetime Events" />      
  </rules>
  
</healthMonitoring>

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.


Comments
AspNetSpy Tuesday, October 13, 2009
This post has been spied by AspNetSpy.com
gravatar Kev Tuesday, October 13, 2009
What's the partial trust story these days with health monitoring? I seem to remember it requiring Full Trust when FW 2.0 shipped. Have they relaxed this in the 3.5/3.5SP1 updates?

Cheers
Kev
gravatar scott Tuesday, October 13, 2009
@Kev:

I can't speak for all the features and providers, but I do know I've logged health events to SQL Server in a medium trust application.
Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!