首页 > Web开发 > 详细

.NetCore 之AOP扩展ExceptionFilter

时间:2020-06-14 00:40:11      阅读:67      评论:0      收藏:0      [点我收藏+]
原文:.NetCore 之AOP扩展ExceptionFilter

ExceptionFilter 主要是为了捕捉系统异常。通常我们会继承 ExceptionFilterAttribute ,实现自定义系统异常捕捉。

版本:.Net Core 3.1

    一、基本创建过程

  1. 新建 .Net Core Mvc 并新建控制器 ExceptionFilterController技术分享图片
  2. Index 控制器肯定会报错,这时候我们就要实现 CustomExceptionFilterAttribute 去捕捉系统异常。

    技术分享图片
     public class CustomExceptionFilterAttribute : ExceptionFilterAttribute
        {
            public override void OnException(ExceptionContext context)
            {
                context.Result = new JsonResult(new
                {
                    Result = false,
                    Message = context.Exception.Message
                });
            }
        }
    技术分享图片

    F5 运行 可知 访问 Index 异常可以正常能捕捉到了。

技术分享图片

   二、CustomExceptionFilterAttribute 优化

现在我们能够捕捉到了异常。接下来我们可以把 CustomExceptionFilterAttribute 在优化一下。我们知道正常网站 可能是Ajax请求或是其他页面请求。所以我们要根据请求类型返回不同的异常信息。比如 ajax 请求返回 json .其他返回 Error页面。可惜的是.NET Core 正对 Request 没有 扩展判断是否是Ajax请求。我们看一下.NET Framework 是如何实现判断Ajax请求的。

技术分享图片

 

 

 所以优化后 CustomExceptionFilterAttribute  

  

 现在想在 CustomExceptionFilterAttribute 使用文件记录一些异常记录,可以把 logger 给注册进来。

  这时在 Action 有2中注册方式:

技术分享图片

 

 

 ServiceFilter 和  TypeFilter 区别在于 一个需要在 全局注册实现,一个不需要。使用 ServiceFilter 在Startup中注册一下

 技术分享图片

 

 

 三、探究 ServiceFilter 和 TypeFilter 原理

技术分享图片

 

 技术分享图片

 

 

我们看到 他们继承的都是一样的。主要是实现 CreateInstance 来获取实例对象。接下来我们模仿自定义一个

  技术分享图片

 

 其实发现 ServiceFilter 和 TypeFilter 无非就是 通过  serviceProvider 获取了实例信息。只不过 TypeFilter 无需在 Startp里面注入了。做了更多的事情。

 

.NetCore 之AOP扩展ExceptionFilter

原文:https://www.cnblogs.com/lonelyxmas/p/13122168.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!