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;
- }
- }
Action执行时间过滤器的更多相关文章
- MVC源码分析 - Action/Result 过滤器(续)
上一篇 看到了Action/Result过滤器的执行顺序: OnActionExecuting -> Action -> OnActionExecuted -> OnResultEx ...
- MVC中使用Action全局过滤器出现:网页无法正常运作 将您重定向的次数过多。解决办法
前言当我们访问某个网站的时候需要检测用户是否已经登录(通过Session是否为null),我们知道在WebForm中可以定义一个BasePage类让他继承System.Web.UI.Page,重写它的 ...
- MVC下用户登录状态校验的问题以及解决方案--------------Action全局过滤器的使用
前言当我们访问某个网站的时候需要检测用户是否已经登录(通过Session是否为null),我们知道在WebForm中可以定义一个BasePage类让他继承System.Web.UI.Page,重写它的 ...
- MVC源码分析 - Action/Result 过滤器执行时机
前面 的篇章, 解析了Action方法的查找, 以及 Authorize, Action, Result, Error 过滤器的加载时机. 也花了两篇去看授权和错误过滤器的使用. 但是对于 Actio ...
- .NET MVC 在action中,过滤器中,或视图中,如何分别获取 当前请求的 控制器/视图/区域 的名字
1)过滤器中的: public class CMSAttribute : FilterAttribute, IAuthorizationFilter { public void OnAuthoriza ...
- 记录每个action执行时间
import org.apache.commons.lang.time.StopWatch; import org.aspectj.lang.JoinPoint; import org.aspectj ...
- 控制器,action, 过滤器, 权限
这个是重点学习对象 控制器 https://www.cnblogs.com/caoyc/p/5671687.html 还有这个 https://www.cnblogs.com/leoo2sk/arc ...
- Asp.Net Mvc Action过滤器(二)
在Mvc中为Action添加过滤器,有两种方式, 一.使用ActionFilterAttribute,简单方式,同时支持Result的过滤处理, 1.可以为空,支持的重写:OnActionExecut ...
- asp.net core MVC 过滤器之ActionFilter过滤器(二)
本系类将会讲解asp.net core MVC中的内置全局过滤器的使用,将分为以下章节 asp.net core MVC 过滤器之ExceptionFilter过滤器(一) asp.net core ...
随机推荐
- 面试题:Java开发中的23种设计模式详解(转)
设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
- easyui-tabs 页签绑定click事件,动态加载jqgrid
.前台代码 <%-- builed by manage.aspx.cmt [ver:] at // :: --%> <%@ Page Language="C#" ...
- numpy.loadtxt() 出现codecError_____ Excel 做矩阵乘法
1) 用 numpy读入csv文件是报错 UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal m ...
- 通过网页发布ios应用。
原文地址:http://www.zhihu.com/question/24304345 两种方法: 1. 测试版本 支持任何类型的开发者帐号,需要在developer后台设置授权deviceID,可以 ...
- Django-Web框架之Hello Django!
1.首先我们配置guest目录下面的settings.py问,将我们的sign应用添加到项目中.如下图所示: 2.我们通过地址栏:http://127.0.0.1:8001/index/访问Djang ...
- datebox 和 datetimebox 添加『清空』按钮
需要引入 easyui-lang-zh_CN.js $(document).ready(function(){ var d_buttons = $.extend([], $.fn.datebox.de ...
- linux新的API signalfd、timerfd、eventfd使用说明
原文:http://www.cfanz.cn/?c=article&a=read&id=46555注意很多当前(2013/8/6)线上运营的Linux内核可能不支持! 三种新的fd加入 ...
- (转)Entity Framework 5.0系列之自动生成Code First代码
原文地址:http://www.cnblogs.com/kenshincui/archive/2013/08/29/3290527.html 在前面的文章中我们提到Entity Framework的“ ...
- 如何将一个用utf-8编码的文本用java程序转换成ANSI编码的文本
jdk有一个关于UTF-8的bug所以加了一句 br.skip(1); bugID: http://bugs.java.com/view_bug.do?bug_id=4508058 public st ...
- cmake安装方法
由于Ubuntu14.04的cmake版本为2.8.x,而如果需要cmake3.x版本时,无法生成makefile,有两种方法可以安装cmake3.10.0: 方法1: sudo apt-get in ...