zoukankan      html  css  js  c++  java
  • Action执行时间过滤器

    public class AccessStatisticsAttribute : ActionFilterAttribute
        {
            /// <summary>
            /// log4net 日志
            /// </summary>
            private static readonly ILog Logger = LogManager.GetLogger(typeof(AccessStatisticsAttribute));
    
            /// <summary>
            /// 该Action对应的权限项名称
            /// </summary>
            private readonly string _actionName;
    
            /// <summary>
            /// 该Action对应的权限项名称
            /// </summary>
            private readonly bool _logResult;
    
            /// <summary>
            /// .ctor
            /// </summary>
            public AccessStatisticsAttribute(string actionName, bool logTheResult = false)
            {
                this._actionName = actionName;
                this._logResult = logTheResult;
            }
    
            /// <summary>
            /// 提供一个入口点用于进行自定义授权检查
            /// </summary>
            /// <param name="filterContext">HTTP 上下文,它封装有关单个 HTTP 请求的所有 HTTP 特定的信息。</param>
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                GetSessionTimer(filterContext).Start();
                base.OnActionExecuting(filterContext);
            }
    
            /// <summary>
            /// OnActionExecuted
            /// </summary>
            /// <param name="filterContext"></param>
            public override void OnActionExecuted(ActionExecutedContext filterContext)
            {
                var stopwatch = GetSessionTimer(filterContext);
                stopwatch.Stop();
                var result = this._logResult ? filterContext.Result.ToJsonNoneFormat() : string.Empty;
    
                if (stopwatch.Elapsed.TotalMilliseconds > ActionHelper.ElapsedMillisecondsLimit)
                {
                    Logger.InfoFormat("OUT : {0} {1} {2}ms {3}s", _actionName, result, (uint)stopwatch.Elapsed.TotalMilliseconds, (uint)stopwatch.Elapsed.TotalSeconds);
                }
    
                base.OnActionExecuted(filterContext);
            }
    
            
            private Stopwatch GetSessionTimer(ControllerContext context, string name = "actionElapse")
            {
                string key = name + "timer";
                if (context.HttpContext.Items.Contains(key))
                {
                    return (Stopwatch)context.HttpContext.Items[key];
                }
    
                var result = new Stopwatch();
                context.HttpContext.Items[key] = result;
                return result;
            }
        }
  • 相关阅读:
    spring-mvc----数据库数据到页面错误--tomcat启动不了
    springmvc注解@RequestMapping
    数据库到jsp页面报错(一)
    IDEA秒退或者一直让填写激活码问题
    spting Boot 创建一个springBoot项目
    SSM框架整合(实现从数据库到页面展示)
    asp.net dbproviderfactory(提供程序工厂模型)多数据库访问
    connectionStrings 中的 providerName 属性
    IDisposable
    利用提示引导语句运行
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/8413860.html
Copyright © 2011-2022 走看看