.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  

 public override void OnException(ExceptionContext context)
        {
            if (!context.ExceptionHandled)
            {
                if (context.HttpContext.Request.IsAjaxRequest())
                {
                    context.Result = new JsonResult(new
                    {
                        Result = false,
                        Message = context.Exception.Message
                    });
                }
                else
                {
                    context.Result = new RedirectResult("/Home/Error");
                }

                context.ExceptionHandled = true;
            } 
        }

  

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

  private readonly ILogger<CustomExceptionFilterAttribute> _logger;

        public CustomExceptionFilterAttribute(ILogger<CustomExceptionFilterAttribute> logger)
        {
            _logger = logger;
        }

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

 

 

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

 

 

 

 三、探究 ServiceFilter 和 TypeFilter 原理

 

 

 

 

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

 public class CustomServiceFilter : Attribute, IFilterFactory, IFilterMetadata
    {
        private readonly Type _FilterType = null;
        public CustomServiceFilter(Type type)
        {
            this._FilterType = type;
        }
        public bool IsReusable => true;

        public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
        {
            return serviceProvider.GetService(this._FilterType) as IFilterMetadata;
        }
    }

  

 

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值