Monday, October 31, 2011

Logging in MVC Part 2 – Health Monitoring

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> :
01<healthMonitoring enabled="true">
02  <eventMappings>
03    <clear />
04    <!-- Log ALL error events -->
05    <add name="All Errors" type="System.Web.Management.WebBaseErrorEvent" startEventCode="0" endEventCode="2147483647" />
06    <!-- Log application startup/shutdown events -->
07    <add name="Application Events" type="System.Web.Management.WebApplicationLifetimeEvent" startEventCode="0" endEventCode="2147483647" />
08  </eventMappings>
09  <providers>
10    <clear />
11    <!-- Provide any customized SqlWebEventProvider information here (such as a different connection string name value -->
12    <add connectionStringName="SampleDatabaseConnectionString" maxEventDetailsLength="1073741823" buffer="false" name="SqlWebEventProvider" type="System.Web.Management.SqlWebEventProvider" />
13  </providers>
14  <rules>
15    <clear />
16    <add name="All Errors Default" eventName="All Errors" provider="SqlWebEventProvider" profile="Default" minInstances="1" maxLimit="Infinite" minInterval="00:00:00" />
17    <add name="Application Events Default" eventName="Application Events" provider="SqlWebEventProvider" profile="Default" minInstances="1" maxLimit="Infinite" minInterval="00:00:00" />
18  </rules>
19</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.

No comments:

Twitter Bird Gadget