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.
Environment variables for testing:
- ROLLBAR_TOKEN - Rollbar token for testing.
Please make sure the following services are available for the various builder modules.
app.UseRollbarExceptionHandler();
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 rollar 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]"
}
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.
You can also post messages/exceptions directly if you so wish.
// Send an exception
var response = await this.Rollbar.SendException(exception);
response.Uuid //Event UUID that can be looked up on the rollbar site.
// Send a message
var response = await this.Rollbar.SendMessage("Hello World!", RollbarLevels.Message);
Although I highly recommend using depdency 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.
}
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.
As of writing this .NET Core does not support walking the stack frames of the exception, means our error messages are pretty weak.
As far as I know log4net is currently implementing .NET Core support.
.NET Core is all about keeping things slim, do we put ASPNETCore code in a different lib?
Would be nice for this to support .NET 4.5.1, no testing and no real effort outside of some basic preprocessor stuff in place.