zoukankan      html  css  js  c++  java
  • ASP.NET Core TypeFilter 使用记录

     

     

    [HttpGet]
    [TypeFilter(typeof(LogActionFilter),Arguments =new object[] { "测试表", "YJYK", "Log_Users", "Insert" , "logCode","我测试一下Aop操作日志" })]
    public IActionResult TestMothod2()
    {
    return Ok();
    }

     

        public class LogActionFilter : Attribute, IActionFilter
        {
            private readonly ILogger<LogActionFilter> _logger;
            private readonly ILog_UsersService _log;
    
    
    
            public LogActionFilter(
                                   ILogger<LogActionFilter> logger,
                                   ILog_UsersService log,
                                   string tableName = "",
                                   string moduleName = "",
                                   string methodName = "",
                                   string logType = "",
                                   string logCode = "",
                                   string logDetail = "")
            {
                _logger = logger;
                _log = log;
                TableName = tableName;
                ModuleName = moduleName;
                MethodName = methodName;
                LogType = logType;
                LogCode = logCode;
                LogDetail = logDetail;
            }
            public string TableName { get; set; }
    
            public string ModuleName { get; set; }
            /// <summary>
            /// 方法名
            /// </summary>
            public string MethodName { get; set; }
            public string LogType { get; set; }
    
    
            /// <summary>
            /// 日志返回码
            /// </summary>
            public string LogCode { get; set; }
    
    
            /// <summary>
            /// 日志描述
            /// </summary>
            public string LogDetail { get; set; }
    
            public void OnActionExecuted(ActionExecutedContext context)
            {
                var logModel = new Model()
                {
                    LogDetail = LogDetail,
                    Url = context.HttpContext.Request.Path,
                    MethodName = MethodName,
                    LogCode = LogCode,
                    LogType = LogType,
                    TableName = TableName,
                    ModuleName = ModuleName
                };
                try
                {
                    _log.AddLogAsync(logModel);
                }
                catch (Exception ex)
                {
    
                    _logger.LogError(ex, "操作日志异常");
                }
            }
            public void OnActionExecuting(ActionExecutingContext context)
            {
            }
        }
    

      通过这种方式就实现了 使用注入服务的 过滤器来记录操作日志

  • 相关阅读:
    远程下载文件并设置进度显示
    python调用函数超时设置
    Ubuntu安装PostgreSQL
    sessionStatMap is full
    LdapTemplate忽略ssl证书
    MySQL5.6 Online DDL
    Mysql5.7编译调试(windows环境)
    Disruptor
    mybatis generator自定义文件后缀名
    maven占位符$变量无法替换
  • 原文地址:https://www.cnblogs.com/litianfeng-net/p/13534241.html
Copyright © 2011-2022 走看看