RollbarDotNet

NuGet Build Maintainability

Rollbar support for your .NET Core projects, relies on dependency injection and hooks up to your ASP.NET Core pipeline for easy use.

Inspired by RollbarSharp, great library, just required too many tweaks to make play with .NET core well in my opinion.

Testing

Environment variables for testing:

  • ROLLBAR_TOKEN - Rollbar token for testing.

Required services

Please make sure the following services are available for the various builder modules.

app.UseRollbarExceptionHandler();

Using in ASP.NET Core

Place the following after your error handling code in Startup.Configure:

app.UseRollbarExceptionHandler();

Place the following in your Startup.ConfigureServices section:

services.AddRollbarWeb(Configuration);

There is also one that doesn't load the builders for building out environment information for web servers (this will not attempt to crawl for server/client/request information):

services.AddRollbar(Configuration);

Hook up the Rollbar configuration using the following code:

services.AddOptions(); // Most apps already are using this, but just in case.
services.Configure<RollbarOptions>(options => Configuration.GetSection("Rollbar").Bind(options));

Configure Rollbar from your appSettings.json file like so:

  "Rollbar": {
    "AccessToken": "[access token here]",
    "Environment": "[named environment here]",
    "Disabled": false
  }

If you want to use the ILogger implementation add this:

    loggerFactory.AddRollbarDotNetLogger(app.ApplicationServices);

In your Configure() method inside of Startup.cs

Getting Occurrence UUID

Getting the occurrence UUID is easy, just get it from the HttpContext Feature collection:

public IActionResult Error()
{
    var response = HttpContext.Features.Get<IRollbarResponseFeature>();
    return Content(response.Uuid);
}

The UUID can be looked up directly via https://rollbar.com/occurrence/uuid/?uuid=[UUID HERE]. This may be really useful if you want to let your users report errors to you, you can include this UUID automatically in the report.

You can check if Rollbar has reported the exception via the IRollbarResponseFeature.Handled boolean.

Calling Directly

You can also post messages/exceptions directly if you so wish.

// Send an exception
var response = await Rollbar.SendException(exception);
response.Uuid //Event UUID that can be looked up on the rollbar site.


// Send a message
var response = await Rollbar.SendMessage("Hello World!", RollbarLevels.Message);

Calling Without Dependency Injection

Although I highly recommend using dependency injection, you can fairly easily configure Rollbar by hand:

var rollbarOptions = Options.Create(new RollbarOptions
{
    AccessToken = "token here",
    Environment = "development"
});
var rollbar = new Rollbar(
    new IBuilder[] {
        new ConfigurationBuilder(rollbarOptions),
        new EnvironmentBuilder(new SystemDateTime()), // SystemDateTime abstracts DateTime for mocking
        new NotifierBuilder()
    },
    new IExceptionBuilder[] {
        new ExceptionBuilder()
    },
    new RollBarClient(rollbarOptions)
);

try
{
    throw new Exception("Testing");
}
catch(Exception exception)
{
    await rollbar.SendException(exception); //Err... everything is async, keep that in mind.
}

Blacklists

Blacklisting will replace variables with asterisks ("**********") when data is sent to Rollbar.

Inside of your appSettings.json you have two options, using plaintext or regular expressions:

  "Rollbar": {
    "AccessToken": "[access token here]",
    "Environment": "[named environment here]",
    "Blacklist": {
      "Regex": [
        "^CreditCard.*$"
      ],
      "Text": [
        "Password"
      ]
    }
  }

Additional Blacklists can be coded by inheriting from the RollbarDotNet.Blacklisters.IBlacklister interface and registering it with your application's dependency injection framework.

To do

Break out into separate libraries

.NET Core is all about keeping things slim, do we put ASP.NET code in a different lib?