C# 未为同步请求调用中间件

C# 未为同步请求调用中间件,c#,asp.net-core,middleware,sentry,C#,Asp.net Core,Middleware,Sentry,我尝试使用SentryDotNet.AspNetCore将未处理的异常记录到sentry 我的startup.cs public void ConfigureServices(IServiceCollection services) { services.AddMvc(); var dsn = "[dsn]"; services.AddSentryDotNet( new SentryClient(

我尝试使用SentryDotNet.AspNetCore将未处理的异常记录到sentry

我的startup.cs

   public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        var dsn = "[dsn]";


        services.AddSentryDotNet(
            new SentryClient(
                dsn,
                new SentryEventDefaults(
                    environment: "test",
                    release: typeof(Startup).Assembly.GetName().Version.ToString(3),
                    logger: "coremvcapp")));
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

        app.UseSentryDotNet(new SentryDotNetOptions { CaptureRequestBody = true });
 //app.Run(async context => { await DoSomethingAsync(context); });
    }
有一个调用中间件的示例,该中间件成功地登录到sentry

private static async Task DoSomethingAsync(HttpContext context)
{
    if (context.Request.Path.HasValue && context.Request.Path.Value.Contains("error"))
    {
        throw new InvalidOperationException("Boom");
    }
    await context.Response.WriteAsync("some stuff");
}
问题在于,如果在控制器操作中引发异常,则不会调用中间件

    public IActionResult LogError()
    {
        throw new Exception("mvc error");
    }
我为中间件的Invoke方法设置了一个断点,在抛出操作中的异常时不会命中该断点

中间件有这个调用方法

    public async Task Invoke(HttpContext context)
    {
        try
        {
            await _next.Invoke(context);
        }
        catch (Exception e) when (_client != null)
        {
            // log stuff

            throw;
        }
    }
我看到它在应用程序启动时被初始化

public SentryDotNetMiddleware(RequestDelegate next, ISentryClient client, SentryDotNetOptions options)
{
    _next = next;
    _client = client;
    _options = options;

}
是否缺少一些东西,以致于在操作中调用中间件的invoke方法来处理未处理的异常

编辑


对我来说,sentry sentry.AspNetCore提供的实现是现成的。我尝试的所有其他包都有其他小问题。

添加中间件的顺序很重要,因为它们是按照添加到管道中的顺序调用的

应该尽早将记录器和错误处理程序添加到管道中

确保在拦截异常的任何中间件之后
使用sentrydotnet()
。否则,
SentryDotNet
中间件将不会看到异常。例如,
app.UseSentryDotNet()

参考文献

您的示例之所以有效,是因为您在添加了sentry中间件之后添加了它

在其他调用中,错误在到达sentry中间件之前发生,因此不会被捕获

参考文献


参考

我一直在检查我是否真的在app.usedeveloperCeptionPage()之后添加了中间件,但没有意识到我也应该在其他中间件之前。这一点现在非常明显。值得注意的是,Sentry对ASP.NET核心有官方支持:(免责声明:我在Sentry工作)
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
    // Make sure middleware that catches exceptions without 
    // rethrowing them is added *before* SentryDotNet
    app.UseDeveloperExceptionPage();

    //Add sentry
    app.UseSentryDotNet(new SentryDotNetOptions { CaptureRequestBody = true });

    // Other middleware, e.g. app.UseMvc()

    app.UseStaticFiles();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}