【问题标题】:Implementing Airbrake into a ASP.NET MVC 5 project在 ASP.NET MVC 5 项目中实现 Airbrake
【发布时间】:2021-05-07 12:25:50
【问题描述】:

我有一个较旧的 .NET 4.8 项目需要使用 Airbrake。该项目使用Unity 作为其IoC 容器,实现标准Repository Service 模式。

几乎没有 ASP.NET 示例。

我想做这样的事情:

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);
        }
    }
}

我认为我需要以某种方式向 ILogger 注册 Airbrake,或者创建自己的日志记录服务。

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;
    }
}

我已经尝试以此为起点:https://github.com/airbrake/sharpbrake/blob/master/docs/asp-net-http-module.md

这很好,但我需要以某种方式对其进行扩展,以便能够在我的服务中使用它,而不仅仅是 .Web 项目。

我知道有 ASP.NET 模块会自动捕获错误,但我想在我认为合适的时候手动记录,并避免每次我想记录错误时都必须调用 airbrake 客户端。

有没有办法做到这一点,还是我完全误解了它应该如何工作?

【问题讨论】:

    标签: c# .net asp.net-mvc airbrake


    【解决方案1】:

    您实际上不需要将其作为 .NET ILogger 的一部分进行连接。我确信有一种方法(可能通过 OWIN),但你没有什么能阻止你像编写任何其他服务一样编写基本的日志记录服务并通过沼泽标准 DI 使用它。答案几乎就在一开始的问题中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-04
      • 2013-11-25
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      相关资源
      最近更新 更多