Share via


Application Health Monitoring (in ASP.NET 2.0 and above)

Vineet Batta here,

A little known but excellent features of ASP.NET is it’s ability to give support teams the ability to monitor the health of ASP.NET applications. In this article I will dwell on out of box features. No custom classes or code to be written. All the configuration setting for enabling Health monitoring (HM) are done in web.config. There are three core sections which you should be aware of while implementing HM;

  • Providers – Event log provider
  • EventMapping - Event mapping provides a friendly name to the events
  • Rules - Rule tying the "All Errors" and "Failure Audits" event mapping to the EventLog Provider

Event Mapping supported are;

<eventMappings>
<add name="All Events"  />
<add name="Heartbeats"    />            
<add name="Application Lifetime Events"  />               
<add name="Request Processing Events"  />               
<add name="All Errors"   />             
<add name="Infrastructure Errors"   />              
<add name="Request Processing Errors"        />          
<add name="All Audits"          />       
<add name="Failure Audits" />               
<add name="Success Audits"  />
</eventMappings>

Complete list of Event mappings, Providers and Rules is provided in web.config (C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG).

An example of Health Monitoring configuration as created for logging messages into Event log. We used the similar configuration as above in one of the applications and were able to completely avoid any code on global.ascx or matter of fact in code behind.

 <healthMonitoring enabled="true">
<!--  Event Log Provider being added. -->
<providers>
  <add name="EventLogProvider" type="System.Web.Management.EventLogWebEventProvider,System.Web,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"/>
 </providers>
  <!--  Event mapping provides a friendly name to the events based on the WebBaseErrorEvent class.  -->
 <eventMappings>
   <add name="All Errors" type="System.Web.Management.WebBaseErrorEvent,System.Web,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" startEventCode="0" endEventCode="2147483647"/>
        <add name="Failure Audits" type="System.Web.Management.WebFailureAuditEvent,System.Web,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" startEventCode="0" endEventCode="2147483647"/>
    </eventMappings>
  <!-- Rule tying the "All Errors" and "Failiure Audits" event mapping to the EventLog Provider.  -->
   <rules>
   <clear/>
  <add name="All Errors Default" eventName="All Errors" provider="EventLogProvider" profile="Default" minInstances="1" maxLimit="Infinite" minInterval="00:00:00" custom=""/>
 <add name="Failure Audits Default" eventName="Failure Audits" provider="EventLogProvider" profile="Default" minInstances="1" maxLimit="Infinite" minInterval="00:00:00" custom=""/>
   </rules>
</healthMonitoring>

You can also support sending emails by choosing a different provider namely – EmailErrorProvider.

Example:

 <add name="EmailErrorProvider"
             type="System.Web.Management.SimpleMailWebEventProvider"
            to= support@xyz.com
           from="HealthMonitoring@xyz.com" 
             buffer="false"
             subjectPrefix="Application failure."
             bodyHeader="This email is generated from my application." />
</providers>

You can also configure  logging into SQL server by configuring the correct provider as - SqlWebEventProvider

   <add connectionStringName="LocalSqlServer"  
maxEventDetailsLength="1073741823" buffer="false" 
bufferMode="Notification" 
name="SqlWebEventProvider" type="System.Web.Management.SqlWebEventProvider,System.Web,Version=2.0.0.0,Culture=neutral,
 PublicKeyToken=b03f5f7f11d50a3a" />

So as you saw how easy and useful this feature is for monitoring the health of applications with so little effort. Latter I may explain how to extend this framework feature with custom classes.

Tip of the day

Recently we upgraded our production environment application bits and were required to encrypt Sql connection string(every one should do it) which has sensitive information like username/password. Soon realized it was working for some of us who were administrators on  the box and rest were getting the following error error message…

 Exception information: 
    Exception type: ConfigurationErrorsException 
    Exception message: Failed to decrypt using provider 'RsaProtectedConfigurationProvider'. Error message from the provider: The RSA key container could not be opened. (E:\webroot\web.config line 56)  

 

After few hrs of looking around we got to the root of the problem and the conclusions was that we need to give access to this RSA Container to all Authenticated users. How?

 aspnet_regiis -pe "connectionStrings" /<your app name> "/"
aspnet_regiis -pa "NetFrameworkConfigurationKey" "everyone"

 

Hope this helps and saves you time.