Welcome to AspAdvice Sign in | Join | Help

Quick N' Easy Health Monitoring in ASP.NET Beta 2

Traditionaly in ASP.NET 1.1 if you wanted to be notified through email of an unhandled exception, you'd have to add code to your Global.asax.cs handling the Application_Error event and subsequently log the error or send yourself an email through smtp.  In .NET 2.0 this functionality is already built in. All you have to do is add a few dozen config lines to the Web.Config and you can have it working in under 10 mins. 

Here's the first set of configuration options you have to add to the Web.Config in the <system.web> section.

  <healthMonitoring enabled="true">
   <providers>
    <add name="CriticalMailEventProvider"
      type="System.Web.Management.SimpleMailWebEventProvider"
         from="
from@somewhere.com"
         to="
to@somewhere.com"
         bodyHeader="Warning!"
         bodyFooter="Investigate ASAP."
         subjectPrefix="Exception Notification."
         buffer="true"
         bufferMode="Critical Notification"
         maxEventLength="4096"
         maxMessagesPerNotification="1"/>
    <remove name="SqlWebEventProvider"></remove>
    <add connectionStringName="Sql2005"
         maxEventDetailsLength="1073741823"
         buffer="false"
         bufferMode="Notification"
         name="SqlWebEventProvider"
         type="System.Web.Management.SqlWebEventProvider,System.Web"/>
   </providers>
   <rules>
    <remove name="All Errors Default"/>
    <remove name="Failure Audits Default"/>
    <add name="All Errors Default" 
         eventName="All Errors"
         provider="SqlWebEventProvider"
         profile="Default"
         minInterval="00:00:30"/>
    <add name="Request Processing Errors"
         eventName="Request Processing Errors"
         provider="CriticalMailEventProvider"
         profile="Default"/>
   </rules>
  </healthMonitoring>

In the Providers section listed above, there are two providers listed: CriticalMailEventProvider and SqlWebEventProvider. The CriticalMailEventProvider sends emails upon errors, while the SqlWebEventProvider creates table entries in the table: aspnet_WebEvent_Events, which is created using aspnet_regsql.exe.

Customization of the CriticalMailEventProvider is pretty straight forward, except for where the mail server is entered. This brings us to the 2nd section required in the Web.Config. Add the following section under the <Configuration> element.

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network">
            <network host="mail.yourhost.com"
                port="25"
                from="
webmaster@yourwebsite.com"/>
        </smtp>
    </mailSettings>
</system.net>

Configuring the smtp host is also straight forward. With this section added, the CriticalMailEventProvider configuration is complete.  The SqlWebEventProvider really only requires you to enter your database name in order to function properly.  In my case, I'm using Sql2005 as my database name.

    <add connectionStringName="Sql2005"
         maxEventDetailsLength="1073741823"
         buffer="false"
         bufferMode="Notification"
         name="SqlWebEventProvider"
         type="System.Web.Management.SqlWebEventProvider,System.Web"/>

You should change the connectionStringName attribute to whatever your connection string is named.  Once done this section's configuration is also complete.  Notice how I have a remove node before the configuration of the SqlWebEventProvider “<remove name="SqlWebEventProvider"></remove>“. This is because .NET provides a default implementation of this provider which uses a default name for the database “LocalSqlServer“.  The remove node allows me to remove that default configuration.

The rules section specifies when a certain provider should be used. 

   <rules>
    <remove name="All Errors Default"/>
    <remove name="Failure Audits Default"/>
    <add name="All Errors Default"
         eventName="All Errors"
         provider="SqlWebEventProvider"
         profile="Default"
         minInterval="00:00:30"/>
    <add name="Request Processing Errors"
         eventName="Request Processing Errors"
         provider="CriticalMailEventProvider"
         profile="Default"/>
   </rules>

looking at the configuration above you can tell that the SqlWebEventProvider is used for “All Errors“, while the CriticalMailEventProvider is only used for “Request Processing Errors.“  By default ASP.NET is configured to log eventName “All Errors” to the EventLog.  Also by default, ASP.NET handles an additional eventName “Failure Audits“ which is again sent to the EventLog.  Since I'm hosted on a shared server I don't get to see the EventLog entries, so there is no point to logging them.  Just as before I've used the remove nodes to remove the default configuration provided by ASP.NET.  I've listed the default configuration for the rules section below:

<rules>
    <add name="All Errors Default"
        eventName="All Errors"
        provider="EventLogProvider"
        profile="Default"
        minInstances="1"
        maxLimit="Infinite"
        minInterval="00:01:00"
        custom="" />
    <add name="Failure Audits Default"
        eventName="Failure Audits"
        provider="EventLogProvider"
        profile="Default"
        minInstances="1"
        maxLimit="Infinite"
        minInterval="00:01:00"
        custom="" />
</rules>

You can get all the default Web.Config by taking a look at the Web.Config stored in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50215\CONFIG.

For even more details about the HealthMonitoring section take a look at Fredrik Normén's blog post How to raise events in ASP.Net 2.0 with the health monitoring feature.

 

Published Monday, May 23, 2005 11:45 PM by richc
Filed under:

Comments

# asp.net 2.0 email provider in web.config

Tuesday, December 20, 2005 9:38 PM by David's Blog

# asp.net 2.0 email provider in web.config

Tuesday, December 20, 2005 9:50 PM by David's Blog
Anonymous comments are disabled