articles

Home / DeveloperSection / Articles / Health Monitoring with ASP.NET MVC

Health Monitoring with ASP.NET MVC

Anonymous User7684 11-Mar-2015

Hi everyone in this article I’m explaining about Implementing Health Monitoring in ASP.NET MVC Applications.

Description:

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.

·     WebHeartbeatEvent

·     WebBaseErrorEvent

·     WebRequestEvent

·     WebRequestErrorEvent

·     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.

·      EventLogWebEventProvider

·      SqlWebEventProvider

·       TraceWebEventProvider

·       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 which events. 

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.

 

<healthMonitoringenabled="true"heartbeatInterval="1000">
      <eventMappings>
        <clear/>
        <addname="MyApplicationHeartbeats"type="System.Web.Management.WebHeartbeatEvent,System.Web,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"
        startEventCode="0"endEventCode="2147483647" />
        <addname="MyApplicationErrors"type="System.Web.Management.WebBaseErrorEvent,System.Web,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"
          startEventCode="0"endEventCode="2147483647" />
      </eventMappings>
      <providers>
        <clear/>
        <addname="EventLogProvider"type="System.Web.Management.EventLogWebEventProvider,System.Web,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"/>
        <addname="DatabaseLogProvider"connectionStringName="MyLogDbConnectionString"maxEventDetailsLength="1073741823"buffer="false"bufferMode="Notification"
             type="System.Web.Management.SqlWebEventProvider,System.Web,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"/>
      </providers>
      <rules>
        <clear/>
        <addname="Rule For Heartbeats"eventName="MyApplicationHeartbeats"provider="EventLogProvider"minInstances="1"maxLimit="Infinite"minInterval="00:01:00"/>
        <addname="Rule For Errors"eventName="MyApplicationErrors"provider="DatabaseLogProvider"profile="Default"minInstances="1"maxLimit="Infinite"minInterval="00:01:00"custom=""/>
      </rules>
</healthMonitoring>

 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.

namespace LearnKnockout
{
    publicclassFilterConfig
    {
        publicstaticvoid RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(newHandleErrorAttribute());
        }
        publicclassMyExceptionHandleErrorAttribute : FilterAttribute, IExceptionFilter
        {
            publicvoid OnException(ExceptionContext filterContext)
            {
                if (!filterContext.HttpContext.IsCustomErrorEnabled)
                    return;
 
                CustomErrorEvent errorEvent = newCustomErrorEvent("MyApplication encountered an error!", this, WebEventCodes.WebExtendedBase + 5, filterContext.Exception);
                errorEvent.Raise();
            }
        }
 
        publicclassCustomErrorEvent : WebErrorEvent
        {
            public CustomErrorEvent(
              string msg, object eventSource,
              int eventCode, Exception exception)
                : base(msg, eventSource, eventCode, exception)
            {
            }
        }
    }
}

I hope this article helpful for you.


Updated 27-Mar-2018
I am a content writter !

Leave Comment

Comments

Liked By