Baklap4 / DotNetAirbrake

Airbrake Notifier for ASP.NET MVC 6

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DotNetAirbrake

Airbrake Notifier for ASP.NET MVC 6

AppVeyor:

Build status

Why

At my current job I was 'living' between Ruby developers which used Airbrake to monitor application errors. We wanted a single solution for error handling so decided to integrate this in .NET MVC 6.

NOTE: We use this with errbit and have not tested with Airbrake self.

Todo

  • Add tests!
  • Add hostname to notify message
  • Add body params to notify message
  • Add action and controller context to notify message
  • Add more environment data to notify message

Usage

Configuration

Add a configuration section in your appsettings.json:

{
  "Airbrake": {
    "Url": "https://airbrake.io/",
    "ProjectId": "my-project-id",
    "ProjectKey": "my-project-key"
  }
}

ASP.NET middleware

Install ASP.NET Core package:

Install-Package DotNetAirbrake.AspNetCore

Register services in the service collection and set the options:

public void ConfigureServices(IServiceCollection services)
{
    // Add Airbrake services
    services.AddAirbrake(options => 
        this.Configuration.GetSection("Airbrake").Bind(options));

    // Add Mcv services
    services.AddMvc();
}

Add the middleware to the pipeline. When an exception is raised in any of the middlewares higher in the pipeline, the exception is automatticly send to your Airbrake service.

public void Configure(IApplicationBuilder app)
{
    // Add Airbrake middleware
    app.UseAirbrake();

    // Add Mvc middelware
    app.UseMvc();
}

Send exception manually

Install client only package:

Install-Package DotNetAirbrake

Create the Airbrake client:

// Create a configuration
var config = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json", true, true)
    .Build();

// Load airbrake options
var airbrakeOptions = new AirbrakeOptions();
config.GetSection("Airbrake").Bind(airbrakeOptions);

// Create logger factory
var loggerFactory = new LoggerFactory();
loggerFactory.AddConsole(config.GetSection("Logging"));

// Create the client
var client = new AirbrakeClient(loggerFactory, airbrakeOptions);

Send your exceptions to your Airbrake service:

try
{
    throw new InvalidOperationException("Test exception");
}
catch (Exception exc)
{
    // Send the exception to airbrake
    await exc.SendToAirbrakeAsync(client);
}

About

Airbrake Notifier for ASP.NET MVC 6

License:MIT License


Languages

Language:C# 78.4%Language:PowerShell 15.9%Language:Shell 5.7%