Health Monitoring with ASP.NET MVC

ASP.NET Health Monitoring has been around for a very long time (.net 2.0).  I personally think it is a feature that has been put on the back burner and forgotten about by most.  Health Monitoring was created to help evaluate the health of an ASP.NET application in a production environment.  It works by recording events to a log location.

Health Monitoring offers multiple options for logging and alerting event messages.  This can be accomplished by logging events to an event provider.  In this example I am going to use a SQL Server database.

Here is a list of the events that can be captured for logging:

  • 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

All of these events derive from five different categories:

  • Application Lifetime Events
  • Request Processing Events
  • Heartbeats
  • All Audits
  • All Errors

Here are the event providers that are available to ASP.NET Health Monitor.

Provider Description
EventLogWebEventProvider Writes events captured by the Health Monitor to the windows event log.
SqlWebEventProvider Writes events captured to a Microsoft SQL server database
WmiWebEventProvider Converts events to WMI events
SimpleMailWebEventProvider/
TemplatedMailWebEventProvider
Events captured are sent to a user by E-Mail
TraceWebEventProvider Events captured are passed to the ASP.NET page tracing system

Since we are logging to a database we need to setup the database first.  The database table name must be aspnet_WebEvent_Events and have a stored procedure with the name of aspnet_WebEvent_LogEvent.  Lucky for us this can be added easily to the database utilizing the aspnet_regsql.exe tool.

Run the following command

aspnet_regsql.exe -E -S .\SqlExpress -d databasename -A w

If you are using Authentication use the this command in stead.

aspnet_regsql.exe -U userID -P password -S serverName -d database -A w

Now we need to configure web.config, here is how mine is setup.  This has everything turned on so if performance because an issue you might need to adjust the times and or remove some options.

<connectionStrings>
    <add name="ApplicationServices" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=EventServer;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
...
<system.web>
...
    <healthMonitoring enabled="true">
      <bufferModes>
            <clear/>
            <add name="Logging" maxBufferSize="1000" maxFlushSize="200" urgentFlushThreshold="800" regularFlushInterval="00:05:00" urgentFlushInterval="00:01:00" maxBufferThreads="1"/>
        </bufferModes>
      <eventMappings>
        <clear />
        <add name="All Events" type="System.Web.Management.WebBaseEvent,System.Web,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" startEventCode="0" endEventCode="2147483647" />
        <add name="All Errors" type="System.Web.Management.WebBaseErrorEvent,System.Web,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" startEventCode="0" endEventCode="2147483647" />
        <add name="All Audits" type="System.Web.Management.WebAuditEvent,System.Web,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" startEventCode="0" endEventCode="2147483647" />   </eventMappings>
      <providers>
        <clear />
        <add connectionStringName="ApplicationServices" maxEventDetailsLength="1073741823" buffer="false" name="SqlWebEventProvider" type="System.Web.Management.SqlWebEventProvider" />
        <add connectionStringName="ApplicationServices" maxEventDetailsLength="1073741823" buffer="true" bufferMode="Logging" name="SqlWebEventProviderBuffered" type="System.Web.Management.SqlWebEventProvider" />
      </providers>
      <profiles>
        <clear/>
        <add name="Audit Logs" minInstances="1" maxLimit="Infinite" minInterval="00:00:05"/>
        <add name="Error Logs" minInstances="1" maxLimit="Infinite" minInterval="00:00:05"/>
        <add name="Application Errors" minInstances="1" maxLimit="Infinite" minInterval="00:00:05"/>
        <add name="Trace" minInstances="1" maxLimit="Infinite" minInterval="00:00:00" />
        <add name="Default" minInstances="1" maxLimit="Infinite" minInterval="00:01:00"/>
      </profiles>
      <rules>
        <clear />
        <add name="All Audits Default" eventName="All Audits" provider="SqlWebEventProviderBuffered" profile="Audit Logs"/>
        <add name="All Errors Default" eventName="All Errors" provider="SqlWebEventProvider" profile="Error Logs" minInstances="1" maxLimit="Infinite" minInterval="00:00:00" />
        <add name="Application Events Default" eventName="All Events" provider="SqlWebEventProvider" profile="Application Errors" minInstances="1" maxLimit="Infinite" minInterval="00:00:00" />
      </rules>
    </healthMonitoring>
  </system.web>

Starting at the top the connection string is how health monitor will connect to the database.

Buffers are used to define when Health Monitoring will raise the events.  SqlWebEventProvider, SimpleMailEventProvider, and TemplatedMailWebEventProvider can take advantage of this.  In the example above All audits use the buffer, errors are done in real time.

Event mappings define what events are being logged.

Providers are how the events will be captured.  Different events can be sent to different sources.  Example could be events and audits are recorded in the database table while errors are sent to someone via email.

Rules map the event to the provider.  Range of event codes to be logged an the profile to be used to capture the event are defined here.

Advertisements
This entry was posted in Development, MVC, Web and tagged , , . Bookmark the permalink.

2 Responses to Health Monitoring with ASP.NET MVC

  1. asma says:

    Thx for the info mate. I was very struggling while coding this. This gonna help me so much . BOokmarked!

  2. Pingback: MVC WEB HTTP, HTTPS Application Monitoring Tools | Software Engineering

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