zoukankan      html  css  js  c++  java
  • .net core 3.1 新增过滤器(Filter)和拦截器(LogInterceptor)

    webapi 项目中,如果需要统一监控或过滤,常用的是借助过滤器 Filter 或拦截器 AOP ,本次我接入了全局的ActionFilter以及ExceptionFilter,以及LogInterceptor。本来还尝试接入AOP框架AspectCore ,不过没调通,先不细说了。

    Filter总共有五种,Authorization Filter,Resource Filter,Exception Filter,Action Filter,Result Filter

    本文我只记录了Exception Filter,Action Filter

    Exception Filter 设置

    新增全局异常过滤器GlobalExceptionFilter.cs,

    当出现异常时进入此方法,可在这针对不同的异常做相关处理并返回指定数据,避免直接把错误暴露给用户 

    public class GlobalExceptionFilter : IExceptionFilter
    {
    public void OnException(ExceptionContext context)
    {
    Exception ex = context.Exception;
    string errMsg = "GlobalExceptionFilter-OnException:" + ex.Message;
    if (context.Exception.GetType() == typeof(ExecuteException))
    {
    //针对不同的自定义异常,做不同处理
    MsgModel<string> msgModel = new MsgModel<string>()
    {
    Status = false,
    Msg = errMsg,
    Errcode = "AA001"
    };
    context.Result = new JsonResult(msgModel);
    context.ExceptionHandled = true;
    }
    else
    {
    context.Result = new JsonResult(errMsg);
    context.ExceptionHandled = true;
    }

    LogHelper.Error(errMsg);
    }
    }

    然后在Startup.cs 注入过滤器

     上面那个AddJsonOptions 是为了解决 JsonResult 中文乱码问题,因为我发现捕获异常后返回中文会乱码

    Action Filter 设置

    新增全局过滤器GlobalActionFilter.cs 

    在方法执行前后,会跳转至以下两个方法,方便追踪接口执行情况 

    public class GlobalActionFilter : IActionFilter
    {
    public void OnActionExecuted(ActionExecutedContext context)
    {
    //LogHelper.Info("OnActionExecuted");
    //执行方法后执行这
    }

    public void OnActionExecuting(ActionExecutingContext context)
    {
    //LogHelper.Info("OnActionExecuting");
    //执行方法前先执行这
    }
    }

    注入方法同上图

    以上是两个过滤器的示例,可针对不同需求选用

    LogInterceptor 设置

    安装Castle.Core,Autofac.Extras.DynamicProxy

    新建LogInterceptor.cs ,继承IInterceptor

    public class LogInterceptor: IInterceptor
    {
    public void Intercept(IInvocation invocation)
    {
    try
    {
    invocation.Proceed();
    Dapper.Logger.LogHelper.logger.Info(invocation.Method.Name);
    }
    catch (Exception ex)
    {
    Dapper.Logger.LogHelper.logger.Error(invocation.Method.Name+ " " + ex.ToString());
    }
    }
    }

    在Startup.cs 新增以下代码

    Filter和 LogInterceptor 可以同时共存,执行顺序是:

    ActionFilter 的OnActionExecuting =》LogInterceptor 的Intercept =》ActionFilter 的OnActionExecuted 

    如果接口有异常,不会跳转LogInterceptor ,而是进入ExceptionFilter,顺序是:

    ActionFilter 的OnActionExecuting =》ActionFilter 的OnActionExecuted =》ExceptionFilter 的OnException

  • 相关阅读:
    .NET的JSNO 序列化跟反序列化
    SQL Server 查询分析器提供的所有键盘快捷方式(转)
    SQL Server 中WITH (NOLOCK)浅析(转潇湘隐者)
    Microsoft Dynamics CRM 2011的组织服务中的RetrieveMultiple方法(转)
    C#对多个集合和数组的操作(合并,去重,判断)
    Silverlight Telerik控件学习:主题Theme切换html教程
    VMware 11安装Mac OS X 10.10
    Android 下载网络图片注意的问题
    对下载文件是否完整的判断方法
    Android实现通用的ActivityGroup(效果类似Android微博客户端主界面),强烈建议不要再使用TabActivity
  • 原文地址:https://www.cnblogs.com/redo/p/12520682.html
Copyright © 2011-2022 走看看