尘叶心繁

.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);


关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢