C#コードは時間の計算を実行して、ここで監視するmvcコントローラの方法です

3207 ワード

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 { /// /// /// 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(); } } }