【发布时间】:2021-12-26 14:56:54
【问题描述】:
我一直在尝试在我的 ASP.Net Core Web API 中设置使用自定义中间件的全局异常处理。我需要使用 Serilog 将它们记录到日志文件中。我尝试了很多,但我无法让它工作。希望这里的朋友能帮帮我。
我的自定义中间件:
public class ExceptionHandlingMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<ExceptionHandlingMiddleware> logger;
public ExceptionHandlingMiddleware()
{
}
public ExceptionHandlingMiddleware(
RequestDelegate next,
ILogger<ExceptionHandlingMiddleware> logger)
{
_next = next ?? throw new ArgumentNullException(nameof(next));
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task InvokeAsync(HttpContext context)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
try
{
await _next.Invoke(context);
}
catch (Exception ex)
{
await HandleExceptionAsync(context, ex);
}
}
private async Task HandleExceptionAsync(
HttpContext context,
Exception exception)
{
try
{
int statusCode = 0;
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
if (exception is null)
{
throw new ArgumentNullException(nameof(exception));
}
this.logger.LogError(exception.ToString());
CustomApiResponse<CustomApiErrorResponse> errorResponse = null;
if (exception is MyException myExceptionInfo)
{
statusCode = myExceptionInfo.StatusCode == default ? (int)HttpStatusCode.InternalServerError : myExceptionInfo.StatusCode;
errorResponse = new CustomApiResponse<CustomApiErrorResponse>()
{
StatusCode = statusCode,
TraceId = context.TraceIdentifier,
Response = new CustomApiErrorResponse()
{
Message = myExceptionInfo.ErrorMessage,
StackTrace = myExceptionInfo.StackTrace
}
};
}
else
{
statusCode = (int)HttpStatusCode.InternalServerError;
const string message = "Unexpected error";
errorResponse = new CustomApiResponse<CustomApiErrorResponse>()
{
StatusCode = (int)HttpStatusCode.InternalServerError,
Response = new CustomApiErrorResponse()
{
Message = message,
},
TraceId = context.TraceIdentifier
};
}
var responseContent = JsonConvert.SerializeObject(errorResponse);
context.Response.ContentType = "application/json";
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseContent);
}
catch (Exception ex)
{
this.logger.LogError(ex.ToString());
throw;
}
}
}
Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
{
//app.UseExceptionHandlerMiddleware();
app.UseExceptionHandler(new ExceptionHandlerOptions
{
ExceptionHandler = new ExceptionHandlingMiddleware().InvokeAsync,
});
//app.UseExceptionHandler(options =>
//{
// options.Run(async context =>
// {
// // Exception Handling Code
// });
//}
}
我哪里错了?我还需要做什么才能让它工作吗?异常会从代码中抛出,但不会被中间件捕获。请朋友们帮帮我。
更新
我使用的是 GraphQL 而不是 REST。所以我的应用程序中没有控制器。这是造成这个问题的原因吗?
【问题讨论】:
-
嗨@Arjun D Nair,
I'm using GraphQL instead of REST. So there are no controllers in my app. Is that a reason for this issue?我想是的。我在 api 控制器中测试。效果很好。 -
嗨@Rena,感谢您的支持。我也提到了我使用 REST 完成的几个旧项目。在那些项目中它工作得很好,但在这个项目中却不行。
-
嗨@Rena,似乎使用 GraphQL 是个问题。我也使用 REST API 示例项目对其进行了测试,并且运行良好。现在我需要弄清楚为什么它在 GraphQL 中不起作用以及如何使它起作用。无论如何,谢谢你为我腾出一些时间。
标签: c# asp.net-core exception asp.net-web-api asp.net-core-middleware