fix(api): GlobalExceptionMiddleware 补异常日志,便于排查静默 500
原实现 catch 异常后只返回通用错误信封,不记任何日志, 导致请求 500 时无法定位根因。补 LogError 记录异常详情+请求路径。
This commit is contained in:
parent
caf3c17fce
commit
4f23e76231
@ -3,6 +3,8 @@ using System.Text;
|
||||
using System.Text.Json;
|
||||
using FluentValidation;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using BuildFlow.Shared.Domain.Errors;
|
||||
using BuildFlow.Shared.Domain.Exceptions;
|
||||
|
||||
@ -37,6 +39,9 @@ public sealed class GlobalExceptionMiddleware(RequestDelegate next)
|
||||
{
|
||||
// 若响应头已发出(如流式输出已写出一部分字节),无法再改写状态码,只能丢弃
|
||||
if (context.Response.HasStarted) return;
|
||||
// 记录异常详情:原实现静默吞掉异常导致 500 无法排查,此处补日志
|
||||
var logger = context.RequestServices.GetService<ILogger<GlobalExceptionMiddleware>>();
|
||||
logger?.LogError(ex, "未处理异常: {Path} {Method}", context.Request.Path, context.Request.Method);
|
||||
await HandleExceptionAsync(context, ex);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user