c#.net MVC operation Monitoring, calculation method/interface/action/page Execution time

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.