supermanpineaplle
supermanpineaplle

Reputation: 5

Implementing Airbrake into a ASP.NET MVC 5 project

I have an older .NET 4.8 project that needs to use Airbrake. The project is using Unity for its IoC container, implementing the standard Repository Service pattern.

There's very little in the way of ASP.NET examples.

I am looking to do something like this:

public static void RegisterTypes(IUnityContainer container)
{
    container.RegisterType(typeof(ILogger<>), typeof(ILogger<>));
    container.RegisterType<IMyService, MyService();
}

public class MyController
{
    private readonly ILogger<MyController> _logger;
    private readonly IMyService _myService;
    
    public MyController(ILogger<MyController> logger, IMyService _myService)
    {
        _logger = logger;
        _myService = myService;
    }

    public MyMethod()
    {
        try
        {
            var x = _myService.DoThis();
        } 
        catch (Exception ex)
        {
            _logger.LogError(e, e.Message);
        }
    }
}

I believe I need to either somehow register Airbrake with ILogger or perhaps create my own logging service.

public class Logging : ILogging
{
    public void LogError(Exception e, string message)
    {
        var airbrake = new AirbrakeNotifier(new AirbrakeConfig
        {
            ProjectId = // pulled from web.config somehow
            ProjectKey = // pulled from web.config somehow
        });

        var notice = airbrake.BuildNotice(ex);
        airbrake.NotifyAsync(notice).Result;
    }
}

I have tried using this as starting point: https://github.com/airbrake/sharpbrake/blob/master/docs/asp-net-http-module.md

This is excellent, but I need to extend it somehow to be able to use it within my services and not just the .Web project.

I know there's the ASP.NET module that will automatically capture the errors but I am wanting to manually log when I see fit, and avoid having to call the airbrake client every time I want to log an error.

Is there a way to do this or am I completely misunderstanding how this should be working?

Upvotes: 0

Views: 88

Answers (1)

supermanpineaplle
supermanpineaplle

Reputation: 5

You don't actually need to wire it up as part of the .NET ILogger. I am sure there is a way (probably via OWIN) but you nothing stops you from writing a basic logging service as you would any other service and using that via bog standard DI. The answer was pretty much in the question to begin with.

Upvotes: 0

Related Questions