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 ...
随机推荐
- hdu 4678 Mine
HDU 4678 把点开空地时会打开的一大片区域看成一块,题目中说到,在一盘游戏 中,一个格子不可能被翻开两次,说明任意两块空地不会包含相同的格子. 那么就可以看成一个组合游戏. 当空地旁边没连任何数 ...
- mac安装nose,command not found:nosetests
mac通过pip install nose失败,看了一下是权限的问题,重新用sudo pip install nose安装,安装成功. 但是执行nosetests时,提示command not fou ...
- javascript总结18:javascript DOM简介
1 HTML DOM 使 JavaScript 有能力对 HTML 事件做出反应.在事件发生时,执行JavaScript 方法,做出交互. 2 格式: onclick=JavaScript脚本 3 H ...
- 停止Nginx服务
查询nginx进程信息 chen@ubuntu:~$ ps -ef |grep nginx root : ? :: nginx: master process /usr/sbin/nginx -g d ...
- 申请免费通配符证书(Let's Encrypt)并绑定IIS(转载)
本文转载自https://blog.csdn.net/qq_41608008/article/details/80491447 什么是 Let's Encrypt? 部署 HTTPS 网站的时候需要证 ...
- xcode工程配置绝对路径与相对路径
1.问题描述 一般我们在xcode里面配置包含工程目录下头文件的时候,都要关联着相对路径和绝对路径,如果只是自己用这个项目,用绝对路径的问题不大,但是如果你把工程发给别人,别人就要在改这个绝对路径,这 ...
- 多线程《七》信号量,Event,定时器
一 信号量 信号量也是一把锁,可以指定信号量为5,对比互斥锁同一时间只能有一个任务抢到锁去执行,信号量同一时间可以有5个任务拿到锁去执行,如果说互斥锁是合租房屋的人去抢一个厕所,那么信号量就相当于一群 ...
- kali linux之搜索引擎Shodan
搜索引擎: 公司新闻动态,重要雇员信息 机密文档/网络拓扑 用户名密码 目标系统软硬件技术架构 Shodan:搜索联网的设备(iot)https://www.shodan.io/ banner:htt ...
- php代码审计8审计文件上传漏洞
文件上传漏洞是指用户上传了一个可执行的脚步文件,并通过此脚本文件获得了执行服务器端命令的能力,这种攻击方式是最直接和有效的,文件上传本身是没问题的,有问题的是文件上传后,服务器怎么处理,解释文件,通过 ...
- python文件操作-修改文件中的内容
一.文件读写有缓冲区 fw = open('nhy','w') fw.write('sdfsdf') fw.flush()# 把缓冲区里面的数据立即写到磁盘上 fw.close() 二.with的用法 ...