Introduction
Once a web application is live, it is very difficult to monitor it round the clock and there can be magnitude of reasons behind any exception / error occurring in application. Therefore, it is always wise to have an integrated error logging and notification mechanism in a web application to find the root cause for an issue and to minimize the error detection time and down time of the web application.
In this tip, we will integrate ELMAH to a web application (using MVC and WebAPI) for error logging (in local DB) as well as to send notification emails.
Background
I have been working on a web application which has both Web API and MVC and required exception logging on both platforms and since both Web API and MVC have different pipelines, ELMAH implementation required specific changes for each platform.
Using the Code
Firstly, ELMAH nuget package is to be installed.
Then, add an mdf database file in App_Start folder.
and execute required SQL
scripts (download from here).
Add connection string to local database in web.config file.
<add name="ELMAHConnectionString" connectionString="Data Source=(LocalDB)\v11.0;
AttachDbFilename=|DataDirectory|\elmah.mdf;Initial Catalog=elmah;Integrated Security=True;
MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
Update the elmah
section of web.config file with connection string and email server details.
<elmah>
-->
<security allowRemoteAccess="false" />
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="ELMAHConnectionString"
applicationName="codeproject demo" />
<errorMail from="fromemail@email.com" to="toemail@email.com"
subject="codeproject elmah error mail" async="false" smtpPort="587"
smtpServer="smtp.emailserver.com" userName="username" password="pwd"/>
</elmah>
Implementation in MVC
Create a class inherited by System.Web.Mvc.HandleErrorAttribute
and overriding OnException
method to log error occurring in MVC application.
public class ElmahHandleErrorAttribute : System.Web.Mvc.HandleErrorAttribute
{
public override void OnException(ExceptionContext context)
{
base.OnException(context);
var e = context.Exception;
var httpcontext = HttpContext.Current;
var signal = ErrorSignal.FromContext(httpcontext);
if (signal != null)
{
signal.Raise(e, httpcontext);
}
}
}
Add class instance to GlobalFilterCollection
in App_Start/FilterConfig.cs.
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new ElmahHandleErrorAttribute());
}
}
Implementation in WebAPI
Create a class inherited by System.Web.Http.Filters.ExceptionFilterAttribute
and overriding OnException
method to log error occurring in WebAPI.
public class ElmahHandleWebApiErrorAttribute : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext context)
{
var e = context.Exception;
RaiseErrorSignal(e);
}
private static bool RaiseErrorSignal(Exception e)
{
var context = HttpContext.Current;
if (context == null)
return false;
var signal = ErrorSignal.FromContext(context);
if (signal == null)
return false;
signal.Raise(e, context);
return true;
}
}
Add class instance to HttpConfiguration
filters in App_Start/WebApiConfig.cs.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Filters.Add(new ElmahHandleWebApiErrorAttribute());
}
}
Error Logs can be viewed by appending elmah.axd to root url at local machine.
Error notifications will automatically be sent to all recipients(semi-colon';' separated) in to
attribute of <errorMail>
tag within <elmah>
section.
References