Setup Rollbar in ASP.NET MVC 5

I’ve previously written about how much I appreciate Rollbar. On my most recent side-project I wanted to integrate it into the ASP.NET MVC5 app I was building.

While Rollbar doesn’t have an official client, they suggest the open source RollbarSharp project.

Installing it is super easy:

Install Library from NuGet

First, you need the bits. Get them with the NuGet package manager, or just run install-package RollbarSharp in your package manager console.

Configuration

RollbarSharp needs to your API key and your environment string (development, production, etc). The best way to set that up is a couple entries in the good ol’ web.config,

web.config
1
2
3
4
<appSettings>
<add key="Rollbar.AccessToken" value="YOUR KEY HERE"/>
<add key="Rollbar.Environment" value="development"/>
</appSettings>

RollbarSharp can automatically read these keys out of the file and you’ll be on your way.

Add an ExceptionFilter to record the errors

Following along with the installation instructions on GitHub, next add an implementer of IExceptionFilter to your project.

RollbarExceptionFilter.cs
1
2
3
4
5
6
7
8
9
10
public class RollbarExceptionFilter : IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
if (filterContext.ExceptionHandled)
return;
(new RollbarClient()).SendException(filterContext.Exception);
}
}

This class will ignore Exceptions that are treated as Handled, but will send all others to Rollbar using the RollbarSharp library. Easy-peasy.

RollbarSharp’s GitHub page suggests adding an instance of this class to GlobalFilters.Filters, but I found this problematic, especially on azure. Azure Websites seem to have a build in custom error page handler that steals the exceptions from flowing to Rollbar.

I seriously thrashed on this for a couple hours and a dozen experimental commits, but finally figured out how it all works.

In MVC5, there’s a nice FilterConfig class already written for you, which adds the offending HandleErrorAttribute that steals your exceptions. Exceptions flow through the filters and are possibly handled and canceled from further processing. This seems to be the case with the HandleErrorAttribute: once it runs, the RollbarExceptionFilter will not get a chance to do its work.

Instead of stuffing the RollbarExceptionFilter into the global filters list, use the FilterConfig class to add it before the HandleErrorAttribute filter.

~/App_Start/FilterConfig.cs
1
2
3
4
5
6
7
8
9
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new RollbarExceptionFilter());
filters.Add(new HandleErrorAttribute());
}
}

Test it out

Can you believe it? That’s all it takes! Create an action that throws an exception and give it a test.

~/Controllers/HomeController.cs
1
2
3
4
5
6
7
8
9
10
11
12
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Throw()
{
throw new Exception("Testing Rollbar integration");
}
}