Web Development & Execution
David Addison
by David Addison
share this
« Back to the Blog

How to automate error reporting in aspnet using health monitoring

How to automate error reporting in aspnet using health monitoring

Tired of writing and maintaining your own error reporting?  ASP.NET Health Monitoring makes receiving, organizing and responding to error reports fun again.  The basics are that Microsoft has provided custom database structure for automatically logging your error reports to your Sql Server database.  In fact it is so easy to setup that you can be up and running in 15 minutes, provided that you already have a database setup.  Here are the basic steps we will cover:

  • Create the database structure.
  • <!--[endif]-->Configure web.config to log exceptions to the database.
  • <!--[endif]-->Optionally add code to web.config to send email reports.

The third step is optional, however as a failsafe it is a good idea to configure your server to also send exception reports via email in case the connection to your database server is lost.

Step 1 – Create the database structure.

This first step assumes that you are connected to your database server, and that the user account you used to connect with has permissions to access Sql Server using Windows Authentication.

Open a command prompt and navigate to:


You will be using asp_regsql.exe to create your database structure.  Asp_regsql.exe also has many other options.  You should run this command to get a full list of command line switches:

Aspnet_regsql  /?

Next to create the database structure you will want to execute this command, replacing {database} with the name of your database:

Aspnet_regsql –S ./MSSQLSERVER –E –A w –d {database}


Step 2 – Configure web.config to log exceptions  to the database.

If you do not have a connection string for the database user that will be recording web events, you should add one.  The user will also need to be granted permission to execute the aspnet_WebEvent_LogEvent stored procedure.  Replace {ConnectionStringName} with the name of your connection string.

Below is the code that you will add to your web.config.  You will see that there are three main areas:

  • <!--[endif]-->eventMappings – which events to log
  • <!--[endif]-->providers – distribution sources for exception reports (eg sql server or email)
  • <!--[endif]-->rules – how the event mappings relate to the providers


<healthMonitoring enabled="true">


          <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"/>



          <clear />

          <!-- Provide any customized SqlWebEventProvider information here (such as a different connection string name value -->

          <add connectionStringName="{ConnectionStringName}" maxEventDetailsLength="1073741823"

                   buffer="false" name="SqlWebEventProvider"

                   type="System.Web.Management.SqlWebEventProvider" />



          <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" />




Step 3 – Optionally add code to web.config to send email reports.

Now that you are logging exceptions to the database hopefully you can feel the power!  ASP.NET works its magic and gives you detailed information regarding every exception.  But just in case your database server goes down, let’s setup email notification.  This assumes that you have an email server with a user account created for routing email.

Add this block to the healthMonitoring providers section, replace {from} and {to}:

        <add type="System.Web.Management.SimpleMailWebEventProvider"

            name="EmailWebEventProvider" buffer="false"

            from="{from}" to="{to}"

            subjectPrefix="Website Error: "

Add this to the rules section:

        <add name="All Errors To E-Mail" eventName="All Errors" provider="EmailWebEventProvider"

                 profile="Default" minInstances="1" maxLimit="Infinite" minInterval="00:00:00" />

And if your application is not already setup to send email, add this to system.net in web.config:







           password="{password}" />”



You should now be receiving email reports regarding exceptions on your website!  For fine tuning and further information please see the Microsoft patterns & practices article titled: How To: Use Health Monitoring in ASP.NET 2.0 .



Thank you for contacting us!

We'll be in touch!

Back Home ×