获取应用程序c#中先前抛出的异常?

时间:2016-02-11 21:41:45

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

我写了一些中间件,在回调中检查响应是否为500.如果是500,我想返回抛出的异常。如何获取应用程序中引发的异常?

Startup.cs

...
 app.UseMiddleware<APIExceptionMiddleware>();

// Add MVC to the request pipeline.
app.UseMvc();
...

APIExceptionMiddleware.cs:

public class APIExceptionMiddleware
    {
        private readonly RequestDelegate _next;

        public APIExceptionMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext context)
        {
            context.Response.OnStarting(
                callback: (state) =>
                {   
                    HttpResponse response = (HttpResponse)state;
                    if (response.StatusCode == 500)
                    {
                        // want to grab exception here turn it into JSON response place it in the response.Body but not sure how I access the exception.
                        return response.WriteAsync("An Error Occured");
                    }
                    return Task.FromResult<object>(null);
                }, state: context.Response);


            await _next.Invoke(context);
        }
    }

因此,当请求进入UseMvc()时,我会抛出一个异常。我可以使用app.UseDeveloperException();并获得一个友好的HTML页面与堆栈跟踪和异常。

我想几乎重复一遍,但为我的应用程序做一个友好的JSON api响应。因此,如果抛出500,我正在使用中间件,我将把它变成一个漂亮的json响应,并通过api请求将其作为我的响应发送出去。我的问题是如何在中间件中获取此异常?

如果UseDeveloperException()正在进行,那么我也不应该这样做吗?

1 个答案:

答案 0 :(得分:2)

查看DeveloperExceptionPageMiddleware的代码......特别是Invoke(HttpContext context)(如下所示)。不使用您当前添加的此默认中间件,而是使用您自己开始的中间件。它与DeveloperExceptionPageMiddleware非常相似:捕获任何异常,但不是返回错误页面,而是根据需要格式化JSON响应。

public async Task Invoke(HttpContext context)
{
    try
    {
        await _next(context);
    }
    catch (Exception ex)
    {
        _logger.LogError(0, ex, "An unhandled exception has occurred while executing the request");

        if (context.Response.HasStarted)
        {
            _logger.LogWarning("The response has already started, the error page middleware will not be executed.");
            throw;
        }

        try
        {
            context.Response.Clear();
            context.Response.StatusCode = 500;

            await DisplayException(context, ex);

            if (_diagnosticSource.IsEnabled("Microsoft.AspNetCore.Diagnostics.UnhandledException"))
            {
                _diagnosticSource.Write("Microsoft.AspNetCore.Diagnostics.UnhandledException", new { httpContext = context, exception = ex });
            }

            return;
        }
        catch (Exception ex2)
        {
            // If there's a Exception while generating the error page, re-throw the original exception.
            _logger.LogError(0, ex2, "An exception was thrown attempting to display the error page.");
        }
        throw;
    }
}