Errors handling and logging in ASP.NET MVC

How to handle errors and exception in ASP.NET MVC applications the right way.
I want the following:
– All the errors and exceptions must be logged
– Logging should be easy to program and it should not influence main code flow
– Error log must contain call stack and time stamp
– User should receive a friendly error page.
Sample project for Visual Studio 2015 can be downloaded here https://github.com/mchudinov/AspMvcErrorHandler
error404


I use the following tools:
Application_Error method to catch all errors in ASP.NET application that were not caught inplace
ELMAH to log all errors
PostSharp to integrate logging as aspect. Read my blog post about using PostSharp for logging
NLog as logging framework. Read my blog post about using NLog.

1. Install and configure NLog

NLog or other logging framework. NLog is good because it can log asynchronously. Here is my blog post Logging in .NET Mono on Linux and Windows using NLog.

2. Add errors handling to Application_Error method and show custom error page

I need one single point in my application to handle exceptions. Exception must be logged and user will see an error page. Error page is in fact an Error view with Error controller behind.

– Remove all customErrors and httpErrors from Web.config
– Create Error view model in Models folder of ASP.NET MVC project

– Create ErrorPageController

– Create Error view

– Add Application_Error method to Global.asax.cs
Application_Error will catch all uncatched exceptions, log exception message with call stack and session data and run ErrorPageController that will show the user friendly error page.

Let’s test now what we did!
Throw an exception somewhere in the application (in a controller or wherever). This should generate a 500 internal server error and an Error page will be shown.
Run application in Visual Studio and open it in Internet Explorer. IE (v11) shows its own 500 error page:

500ie

Open same page in FireFox. Our custom error page will be shown:
500firefox

Navigate to a non existing path will generate a 404 error page:
404firefox

Note! Errors occurred in Application_Start will be logged but user will not be redirected to an Error page because Response object does not existed in Application_Start yet.

3. Use aspects for logging and exception handling

I use to separate aspects for logging and for exception logging.
– Install PostSharp. Here is my blog post Logging in .NET with AOP using PostSharp

– Add exception log aspect

– Add log aspect.
It will logg all methods on entry and on leaving when log level is set to DEBUG. Can be quite helpful when troubleshooting.

Use these aspects on every class that you need exceptions logging and debug logging:

AspectPriority modifier is needed to avoid execution priority conflict since both aspects implement OnException method.

Log example with on entry and on leaving events:

4. Use ELMAH

ELMAH is Error Logging Modules and Handlers. Here is a simple ELMAH tutorial. With ASP.NET MVC applications I use a special NuGet package Elmah.MVC. Just install and use it.