zoukankan      html  css  js  c++  java
  • ASP.NET Core ---异常处理

    一、局部异常处理:

           在Action里面catch

    二、全局异常处理:

           1、默认的异常处理配置:

            默认配置在StartUp文件的Configure中注册错误处理,显示开发者错误页面:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
     
    
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }
    }

           2、 使用 UseExceptionHandler 处理

            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
    
                //app.UseDeveloperExceptionPage();//返回错误页面,如果做api就不适合了
                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)
                        {
                            //记录日志
                        }
                        await context.Response.WriteAsync(ex?.Error?.Message ?? "an error occure");
                    });
    
    
                });
    
                app.UseHttpsRedirection();
                app.UseMvc();
                
            }

    封装成扩展方法,使用app.use...调用:

        public static class ExceptionHandlingExtensions
        {
            public static void UseMyExceptionHandler(this IApplicationBuilder app,ILoggerFactory loggerFactory)
            {
                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("BlogDemo.Api.Extensions.ExceptionHandlingExtensions");
                            logger.LogDebug(500, ex.Error, ex.Error.Message);
                        }
                        await context.Response.WriteAsync(ex?.Error?.Message ?? "an error occure");
                    });           
                });
            }
        }
            public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory loggerFactory)
            {
    
                //app.UseDeveloperExceptionPage();//返回错误页面,如果做api就不适合了
                app.UseMyExceptionHandler(loggerFactory);
    
                app.UseHttpsRedirection();
                app.UseMvc();
                
            }
  • 相关阅读:
    failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected 排坑指南
    使用labelme制作自己的数据集
    linux安装anaconda3 conda: command not found
    windows端运行.sh脚本
    安装easydict
    tensorflow安装排坑笔记
    Dev怎么调试,怎么调试不了???
    NameError: name 'QApplication' is not defined 的解决办法
    绝对路径和相对路径
    关于Pycharm总是Indexing很久的问题
  • 原文地址:https://www.cnblogs.com/fuyouchen/p/9584456.html
Copyright © 2011-2022 走看看