WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C#代码执行耗时计算,此处是监测的mvc控制器方法

Posted on 2018-10-16 10:54  WebEnh  阅读(810)  评论(0编辑  收藏  举报

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011511086/article/details/78710980
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Mvc;

namespace Mall.Site
{

/// <summary>
/// 执行耗时监测
/// </summary>
public class StopwatchFilter : ActionFilterAttribute
{
Stopwatch wat = new Stopwatch();
Stopwatch swAsync = new Stopwatch();

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
swAsync.Stop();
if (swAsync.ElapsedMilliseconds>0)
{
string msg = string.Format("页面{0},线程id={1},Action执行时间{2}毫秒", filterContext.HttpContext.Request.RawUrl, Thread.CurrentThread.ManagedThreadId, swAsync.ElapsedMilliseconds);
FrameWork.log4net.LogHelper.LogInfo(msg);
}
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
swAsync.Reset();
swAsync.Start();
}


public override void OnResultExecuted(ResultExecutedContext filterContext)
{
wat.Stop();
if (wat.ElapsedMilliseconds>0)
{
string msg = string.Format("页面{0},线程id={1},View执行时间{2}毫秒", filterContext.HttpContext.Request.RawUrl, Thread.CurrentThread.ManagedThreadId, wat.ElapsedMilliseconds);
FrameWork.log4net.LogHelper.LogInfo(msg);
}
}
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
wat.Reset();
wat.Start();
}
}
}
---------------------
作者:围梃狄羽
来源:CSDN
原文:https://blog.csdn.net/u011511086/article/details/78710980?utm_source=copy
版权声明:本文为博主原创文章,转载请附上博文链接!