1. Establish a timingactionfilter filter
public class Timingactionfilter:actionfilterattribute {public override void OnActionExecuting (actionexecuting Context filtercontext) {Gettimer (Filtercontext, "action"). Start (); Base. OnActionExecuting (Filtercontext); } public override void OnActionExecuted (ActionExecutedContext filtercontext) {Gettimer (Filtercon Text, "action"). Stop (); Base. OnActionExecuted (Filtercontext); } public override void Onresultexecuted (ResultExecutedContext filtercontext) {var rendertimer = Gettimer (Filtercontext, "render"); Rendertimer.stop (); var Actiontimer = Gettimer (filtercontext, "action"); if (actiontimer.elapsedmilliseconds >= | | rendertimer.elapsedmilliseconds >=) {Lo Ghelper.writelog ("Operation Monitoring (" + filtercontext.routedata.values["Controller"] + ")", String.Format ("" {0} "-" {1} ", execute: {2}ms, Render: {3}MS", filtercontext.routedata.values["Controller"], FilterContext.RouteData.Valu Es["Action"], Actiontimer.elapsedmilliseconds, Rendertimer.elapsedmillisecon DS)); } base. Onresultexecuted (Filtercontext); } public override void Onresultexecuting (ResultExecutingContext filtercontext) {Gettimer (Filterc Ontext, "Render"). Start (); Base. Onresultexecuting (Filtercontext); } private Stopwatch Gettimer (controllercontext context, string name) {string key = "__timer__" + Name if (context. HttpContext.Items.Contains (key)) {return (Stopwatch) context. Httpcontext.items[key]; } var result = new Stopwatch (); Context. Httpcontext.items[key] = result; return result; } }
Where Loghelper.writelog is my write log file generic class, modified to your own can.
3. Add features to controller or action
4, execution time or rendering time greater than 100ms will be recorded
I've got a couple of execution times here. 7, 800ms is because every time the project is rebuilt, so the first time will be so slow, regardless of it is:)
This article from: http://www.itdos.com/Mvc/20150324/0126300.html reprint please keep this link, thank you!
c#.net MVC operation Monitoring, calculation method/interface/action/page Execution time