代码之家  ›  专栏  ›  技术社区  ›  atoms

正在注册应用程序。UseExceptionHandler导致中间件未接收到异常c#net7.0

  •  0
  • atoms  · 技术社区  · 3 年前

    我有一个简单的项目,我正试图将其配置为向外部错误报告服务报告异常。

    在抛出异常时,我希望蜜獾中间件调用wait client.NotifyAsync(exception) 。这在我注册应用程序之前一直有效。UseExceptionHandler(“/error”)。

    我可以看到调用到达中间件Invoke方法,但是当在等待的上下文中抛出expcption时,它不会返回到Invoke方法。

    请有人解释为什么会出现这种情况,以及我能做些什么来确保HoneybadgerMiddleware在expection时首先被调用,报告expection,然后重新抛出异常,以便UseExceptionHandler控制器接收它。

    public class Startup
    {
    
        public Startup(){}
    
        public void ConfigureServices(IServiceCollection services)
        {
            // add honeybadger startup filter and client
            services
                .AddSingleton<IStartupFilter, HoneybadgerStartupFilter>()
                .AddScoped<IHoneybadgerClient, HoneybadgerClient>(context =>
                    (HoneybadgerClient)HoneybadgerSdk.Init(
                        new HoneybadgerOptions(_config.GetValue<string>("Honeybadger:ApiKey") ?? "")));
    
            services.AddUmbraco(_env, _config)
                .AddBackOffice()
                .AddWebsite()
                .AddComposers()
                .Build();
        }
    
    
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {       
            app.UseExceptionHandler("/error");
        
            app.UseUmbraco()
                .WithMiddleware(u =>
                {
                    u.UseBackOffice();
                    u.UseWebsite();
                })
                .WithEndpoints(u =>
                {
                    u.UseInstallerEndpoints();
                    u.UseBackOfficeEndpoints();
                    u.UseWebsiteEndpoints();
                });
        }
    }
    
    
    public class HoneybadgerStartupFilter : IStartupFilter
    {
        public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
        {
            return builder =>
            {
                // builder.ApplicationServices.GetService<DiagnosticListener>()?.SubscribeWithAdapter(new DiagnosticSubscriber());
                builder.UseMiddleware<HoneybadgerMiddleware>();
                next(builder);
            };
        }
    
    
    
    public class HoneybadgerMiddleware
    {
        private readonly RequestDelegate _next;
    
        public HoneybadgerMiddleware(RequestDelegate requestDelegate)
        {
            _next = requestDelegate;
        }
    
        public async Task Invoke(HttpContext context, IHoneybadgerClient client)
        {
            try
            {
                await _next(context);
            }
            catch (Exception exception)
            {
                await client.NotifyAsync(exception);
                // re-throw the original exception
                throw;
            }
        }
    }
    
    1 回复  |  直到 3 年前
        1
  •  1
  •   Guru Stron    3 年前

    你的 IStartupFilter 将在之前注册 UseExceptionHandler 所以当它被调用的时候,异常已经被处理了。您可以尝试在异常过滤器之后“手动”注册中间件:

    app.UseExceptionHandler("/error");
    app.UseMiddleware<HoneybadgerMiddleware>();
    
    推荐文章