Skip to content

anfomin/RaygunCore

Repository files navigation

Raygun provider for .NET Core 2.0

Standart Raygun4Net library does not work well with .NET Core 2.0. So I've created library for new architecture:

  • Built with interfaces and works via default dependency injection;
  • As result, services can be easily extended or replaced;
  • You can work via IRaygunClient directly or use RaygunLogger or intergrate handler into ASP.NET Core pipeline.

Installation via NuGet

Package Description NuGet
RaygunCore Raygun client and logger NuGet
RaygunCore.AspNetCore Handler for ASP.NET Core request pipeline NuGet

How to use with ASP.NET Core

Register services in Startup class:

public void ConfigureServices(IServiceCollection services)
{
    // configure with API KEY
    services.AddRaygun("_API_KEY_")
        .WithHttp();

    // or with options
    services.AddRaygun(opt => opt.ApiKey = "_API_KEY_")
        .WithHttp();

    // or with configuration section
    services.AddRaygun(configuration)
        .WithHttp();
}

Method AddRaygun() registers only minimal required services. So then you can request IRaygunClient service and send errors:

public async Task<string> ActionInController([FromServices]IRaygunClient raygun)
{
    try
    {
        // some code
    }
    catch (Exception ex)
    {
        await raygun.SendAsync(ex);
    }
    return "OK";
}

Method WithHttp() in application services registration adds pipeline handler so any exception in request is automatically sent to Raygun.

How to use with logging

You can register Raygun logger provider:

WebHost.CreateDefaultBuilder(args)
    .UseStartup<Startup>()
    .ConfigureLogging((context, logging) => logging
        .AddRaygun(r => r
            .Configure(opt => opt.ApiKey = "_API_KEY_")
            .WithHttp()
        )
    );

Then just use logger:

public async Task<string> ActionInController([FromServices]ILogger<MyController> logger)
{
    logger.LogError(0, "My error message");
}

License

This package has MIT license. Refer to the LICENSE for detailed information.