簡體   English   中英

在 ASP.NET MVC 5 項目中實施 Airbrake

[英]Implementing Airbrake into a ASP.NET MVC 5 project

我有一個需要使用 Airbrake 的舊.NET 4.8項目。 該項目使用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 客戶端。

有沒有辦法做到這一點,還是我完全誤解了這應該如何工作?

您實際上不需要將其作為 .NET ILogger 的一部分進行連接。 我確信有一種方法(可能通過 OWIN),但你沒有什么能阻止你像編寫任何其他服務一樣編寫基本的日志記錄服務並通過沼澤標准 DI 使用它。 答案幾乎就在一開始的問題中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM