• 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)
            {
            }
        }
    

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

  • 相关阅读:
    Linux驱动下的platform总线架构(转)
    一生中很值得看的30个故事之一断箭
    学习嵌入式LINUX系统的笔记和体会
    DIY:自己动手做一个迷你 Linux 系统
    linux里面i386 i686 i486 i586代表什么?是什么意思
    菜鸟编译Linux内核
    LINUX核心重编与升级
    ARM 内核移植中常见的错误
    Linux 2.6.19.x 内核编译配置选项简介
    基于S3C2410平台移植Linux 2.6内核指南
  • 原文地址:https://www.cnblogs.com/litianfeng-net/p/13534241.html
Copyright © 2020-2023  润新知