Health Monitoring with ASP.NET MVC
Hi everyone in this article I’m explaining about Implementing
Health Monitoring in ASP.NET MVC Applications.
As you introduce more and more functionalities into your
website, the chance of exception occurrence in a real production scenario also
grows. Some people take time to notify you of the issue that they are facing in
your website but many don’t bother and simply stop visiting your site.
Health Monitoring is a process which allows you to proactively
monitor the health of your website that is running in production without
waiting for the end user to come back and say that the website is not working
as expected. There can be multiple reasons for your website not to perform as
expected and they may look like they're working perfectly fine in the
development environment. Some of them could be thread dead locks, memory leaks,
edge cases that are not handled by your code, etc.
Health Monitoring Features:
Asp.Net health monitoring is a powerful application
monitoring feature introduced with Asp.Net 2.0 by Microsoft. It can be
configured directly through the web.config entries and does not require any
code level changes. The key thing is the amount of information provided by each
health monitoring event.
ASP.NET health monitoring can be customized in such a way
that any event can be monitored and captured information can be sent to
multiple source based on the configuration. At a later point of time this data
can be used by the experts to do the analysis.
1. HealthMonitoring: This is the parent tag in the configuration file and has the
enable attribute, which allows the entire feature to be enabled or disabled for
a particular application.
2. EventMappings: List of events that are to be monitored can be
listed out in this section with unique names. Following are a few in-built
health monitoring events under System.Web.Management.
WebApplicationLifetimeEvent and so on.
3. Providers: These are the handlers for handling the event
scenarios and sending the captured data to a defined destination. It can either
be a database logging provider, event log provider, email provider, etc.
Following are some of the in-built providers.
SimpleMailWebEventProvider and so on.
4. Roles: This where the eventMappings and providers are tied up
together. Based on these roles, Asp.Net decides which providers to use for
Implement Health Monitoring in ASP.NET MVC:
In this section let us implement health monitoring for a
sample Asp.Net MVC application. Create an Asp.Net MVC application in .net
framework 4.5 using Visual Studio 2012. The requirement of the health
monitoring is to log all of the application heartbeats to the event log and
capture all of the exception details to a SqlDatabase. The following code does
exactly what is required.
<healthMonitoring enabled="true" heartbeatInterval="1000">
<add name="MyApplicationHeartbeats" type="System.Web.Management.WebHeartbeatEvent,System.Web,Version=184.108.40.206,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"
startEventCode="0" endEventCode="2147483647" />
<add name="MyApplicationErrors" type="System.Web.Management.WebBaseErrorEvent,System.Web,Version=220.127.116.11,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"
startEventCode="0" endEventCode="2147483647" />
<add name="EventLogProvider" type="System.Web.Management.EventLogWebEventProvider,System.Web,Version=18.104.22.168,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"/>
<add name="DatabaseLogProvider" connectionStringName="MyLogDbConnectionString" maxEventDetailsLength="1073741823" buffer="false" bufferMode="Notification"
<add name="Rule For Heartbeats" eventName="MyApplicationHeartbeats" provider="EventLogProvider" minInstances="1" maxLimit="Infinite" minInterval="00:01:00"/>
<add name="Rule For Errors" eventName="MyApplicationErrors" provider="DatabaseLogProvider" profile="Default" minInstances="1" maxLimit="Infinite" minInterval="00:01:00" custom=""/>
Workaround for Exception Logging with CustomErrors in MVC:
One thing to note in ASP.NET MVC health monitoring is that
the exception health monitoring will stop working if the custom error section
in web.config is set to On/RemoteOnly. There is work around where a custom
HandleErrorAttribute can be created and applied in the FilterConfig.cs shown in
the following code.
public class FilterConfig
public static void
public class MyExceptionHandleErrorAttribute : FilterAttribute, IExceptionFilter
public void OnException(ExceptionContext filterContext)
CustomErrorEvent errorEvent = new CustomErrorEvent("MyApplication encountered an error!", this, WebEventCodes.WebExtendedBase + 5,
public class CustomErrorEvent : WebErrorEvent
string msg, object eventSource,
int eventCode, Exception exception)
: base(msg, eventSource, eventCode, exception)
I hope this article helpful for you.