Integrating with ASP.NET MVC
Creating an MVC error handler for the .NET SDK
Overview
This plugin integrates Rollbar into your ASP.NET MVC app using the .NET SDK. This guide will show you how to add a global exception handler to catch all uncaught errors in your MVC app and how to send custom items to Rollbar.
For an in-depth tutorial with a sample application, see our blog Error monitoring in ASP.NET MVC.
Installation
- Log into your Rollbar account dashboard. Go to Settings → Project Access Tokens and then copy the token.
- Install the Rollbar .NET SDK using NuGet Package Manager. You can use the UI in Visual Studio under Tools->NuGet Package Manager and then click Package Manager Console or Manage NuGet Package for Solution.
Alternatively, to install from the console, run the command below:
Install-Package Rollbar
- Create an exception filter to handle exceptions.
namespace Rollbar.App_Start
{
public class RollbarExceptionFilter : IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
if (filterContext.ExceptionHandled)
{
return;
}
RollbarLocator.RollbarInstance.Error(filterContext.Exception);
}
}
}
- To handle exceptions globally, we'll set up a
FilterConfig
to add our custom filter to theGlobalFilterCollection
. You can find the FilterConfig.cs file inside the App_Start folder.
namespace Rollbar
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new RollbarExceptionFilter());
}
}
}
- Register it in the
GlobalFilters
in theApplication_Start
method and then configure the Rollbar SDK with the access token you received from step 1. You can find theApplication_Start
method in the file Global.asax, which is in the root application directory.
protected void Application_Start()
{
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
const string postServerItemAccessToken = "Your Post Server Access Token";
RollbarLocator.RollbarInstance.
Configure(new RollbarConfig(postServerItemAccessToken){
Environment = "production"
}
);
}
Usage
In the installation section, we created a global error handler to log errors in Rollbar. Additionally, we offer several methods to log errors, warnings, debug messages and more.
Logging a Specific Message
You can log your own messages anywhere in your app. For example, to log a debug
message:
RollbarLocator.RollbarInstance
.Info("Basic info log example.")
.Debug("First debug log.")
.Error(new NullReferenceException())
.Error(new Exception("outer", new NullReferenceException("inner")))
Adding Context Information
You can pass user information as additional context like this:
Person person = new Person("123");
person.Email = "[email protected]";
person.UserName = "John Doe";
RollbarLocator.RollbarInstance.Config.Person = person;
RollbarLocator.RollbarInstance.Error(e);
Exception Logging
You can also report errors to Rollbar inside a try-catch block. For example, to send any type of Exception
add the following code:
try
{
string test = null;
test.toString();
}
catch(Exception e)
{
RollbarLocator.RollbarInstance.error(e);
}
Configuration
Configuring your access token when you initialize your app enough for basic configuration.
const string postServerItemAccessToken = "Your Post Server Access Token";
RollbarLocator.RollbarInstance.
Configure(new RollbarConfig(postServerItemAccessToken){
Environment = "production"
}
);
Related projects
This project is a wrapper for our .NET SDK
Help / Support
If you run into any issues, please email us at [email protected]
For bug reports, please open an issue on Github.
Updated about 1 year ago