1. Establish a timingactionfilter filter
2, the code is as follows:
public class timingactionfilter : actionfilterattribute { public override void onactionexecuting ( Actionexecutingcontext filtercontext) { gettimer (filtercontext, "action"). Start (); base. OnActionExecuting (Filtercontext); } public override void onactionexecuted (ActionExecutedContext Filtercontext) { gettimer (filtercontext, "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 >= 100 | |  RENDERTIMER.ELAPSEDMILLISECONDS >= 100) { loghelper.writelog ("Operational monitoring (" + filtercontext.routedata.values[" Controller "] + ") ", string.format ( "{0}"-"{1}", execute: {2}ms, Render: {3}MS ", filtercontext.routedata.values["Controller"], filtercontext.routedata.values["Action"], actiontimer.elapsedmilliseconds, rendertimer.elapsedmilliseconds ); } base. Onresultexecuted (Filtercontext); } public override void onresultexecuting (ResultExecutingContext Filtercontext) { gettimer (filtercontext, "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:)
c#.net MVC operation Monitoring, calculation method/interface/action/page Execution time