.net core 自定义Error规则
电脑版发表于:2019/10/28 14:23
创建并添加自定义的错误处理事件
namespace AiDaSiBlog.API.Extensions { public static class ExceptionHandlingExtensions { public static void UseMyExceptionHandler(this IApplicationBuilder app,ILoggerFactory loggerFactory) { #warning 重写异常业务 app.UseExceptionHandler(builder => { builder.Run(async context => { context.Response.StatusCode = StatusCodes.Status500InternalServerError; context.Response.ContentType = "application/json"; var ex = context.Features.Get<IExceptionHandlerFeature>(); if (ex != null) { var logger = loggerFactory.CreateLogger("AiDaSiBlog.API.Extensions.ExceptionHandlingExtensions"); logger.LogError(500,ex.Error,ex.Error.Message); } await context.Response.WriteAsync(ex?.Error?.Message ?? "An Error Occurred."); }); }); } } }
Startup.cs 类的 Configure 中
app.UseMyExceptionHandler(loggerFactory);