Logging in MVC Part 2 – Health Monitoring

This is part 2 of the MVC Logging series. Other articles in the series are:

Introduction

This is the second article in a series. The first article showed how to set up ELMAH to run on an MVC website.

The main focus of ELMAH is to log unhandled exceptions but there are many events that it can’t log for us. That is where ASP.NET Health monitoring comes in!

Health Monitoring

To log such things as when a website starts up, shuts down, recompiles etc the best tool to use is ASP.NET Health Monitoring. Health monitoring is closely tied to the ASP.NET runtime and can log many events that happen on your website:

For those not familiar with what Health Monitoring here is a list of events that can be tracked using it:

* Application starts and stops
* Failed logins and unhandled exceptions
* “Heartbeats”
* Successful and failed login attempts through Membership
* Successful and failed URL and ACL authorizations by authenticated users
* Valid and expired forms authentication tickets
* View state validation failures
* Compilation errors
* Configuration errors
* Unhandled exceptions
* Request validation failures
* Anything that causes request to abort
* Requests queued, processing, or rejected
* Specific or periodic monitoring event
* Process start time and more

One of the best articles to get started with Health Monitoring is the official page here:

http://www.asp.net/hosting/tutorials/logging-error-details-with-asp-net-health-monitoring-cs

Quick setup guide

1. Setup the database we are using so that we can store health monitoring events.

2. Modify the web.config to include a health monitoring section.

Setting up the database

You have 2 choices here. You can choose to store the health monitoring events in a separate database to your normal website or you can choose to store everything in the one database.

When you create a new MVC project in VS2010, a database is already created for you in the app_data folder which should be setup and ready to go. You may have to select “Show all files” to see the database as it is hidden by default.

However, for the sample application we are building in this series I chose to store everything in one database as it is easier to manage and move around one database instead of two.

* Browse to C:\WINDOWS\Microsoft.NET\Framework\<versionNumber>
* Run the following command :
aspnet_regsql.exe -E -S [machinename]\[instancename] -d [databasename] -A all

-E Use Windows authentication
-S server
-d database
-A Select options to install

You should now see all of the aspnet tables in your database:

If you get stuck setting up your database or want to see the full options available see the following article:

http://msdn.microsoft.com/en-us/library/x28wfk74.aspx

Modifying the web.config file

Once the database is configured add the following to your web.config file. The parent tag is <system.web> :


<healthMonitoring enabled="true">
  <eventMappings>
    <clear />
    <!-- Log ALL error events -->
    <add name="All Errors" type="System.Web.Management.WebBaseErrorEvent" startEventCode="0" endEventCode="2147483647" />
    <!-- Log application startup/shutdown events -->
    <add name="Application Events" type="System.Web.Management.WebApplicationLifetimeEvent" startEventCode="0" endEventCode="2147483647" />
  </eventMappings>
  <providers>
    <clear />
    <!-- Provide any customized SqlWebEventProvider information here (such as a different connection string name value -->
    <add connectionStringName="SampleDatabaseConnectionString" maxEventDetailsLength="1073741823" buffer="false" name="SqlWebEventProvider" type="System.Web.Management.SqlWebEventProvider" />
  </providers>
  <rules>
    <clear />
    <add name="All Errors Default" eventName="All Errors" provider="SqlWebEventProvider" profile="Default" minInstances="1" maxLimit="Infinite" minInterval="00:00:00" />
    <add name="Application Events Default" eventName="Application Events" provider="SqlWebEventProvider" profile="Default" minInstances="1" maxLimit="Infinite" minInterval="00:00:00" />
  </rules>
</healthMonitoring>

Don’t forget to change the name of the connection string to the one used by your website.

Viewing the results

ASP.NET Health Monitoring does not come with any way to view the events logged in the database so fire up your website and query the table in your database to ensure that the events are being logged.

Conclusion

Well that’s the end of this article. It was quite short in length but in the upcoming articles I will show how we can display all of these logged events in a much nicer format and throw in some bells and whistles.

Download

The source code for part 2 is on the Downloads tab of the associated Codeplex website

About these ads
Tagged with: , , , ,
Posted in ASP.NET MVC
One comment on “Logging in MVC Part 2 – Health Monitoring
  1. Terry says:

    Hi Darren,

    I’ve come across your series by trying to learn more about ELMAH. Without digging into its actual source code, I’m trying to determine what makes it so much better than simply using Health Monitoring’s (HM) Unhandled Exception process. I know off the top of my head that you don’t get the inbuilt web access to the errors, but surely whipping up a few grids in MVC is trivial.

    We are baking up a new ‘framework’ for our products and are currently leveraging HM, but I’m scared I’m missing the boat. Do you have any opinions of why ELMAH is a clear choice over HM?

    Thanks in advance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: