【问题标题】:How to log the request url when an error occurs in asp net core?asp net core发生错误时如何记录请求url?
【发布时间】:2021-10-20 09:53:52
【问题描述】:

为什么这段代码只有在没有发生错误时才会执行?

app.Use(async (context, next) => {
    Console.WriteLine(context.RequestAborted.IsCancellationRequested);

    if(context.Response.StatusCode < 200 || context.Response.StatusCode > 299) {
        Console.WriteLine();
        Console.WriteLine(context.Request.Method);
        Console.WriteLine(context.Request.Path);
    }
    
    await next.Invoke();
});

当发生未处理的异常时,是否有一种简单的方法来记录请求的 url 和可选的正文?

【问题讨论】:

    标签: c# asp.net-core error-logging


    【解决方案1】:

    我建议你看看this 了解如何提取请求正文,然后在发生异常时使用类似

    // remember to put this as high in the pipeline as you need
    app.Use(async (context, next) => {
                    try
                    {
                        await next.Invoke();
                    }
                    catch (Exception e)
                    {
                        // Extract Http request body, logging out the exception,... everything that you see necessary
                    }
                });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-18
      • 2019-03-10
      • 2021-07-21
      • 2022-10-19
      • 1970-01-01
      • 2021-08-19
      • 2017-03-23
      • 1970-01-01
      相关资源
      最近更新 更多