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

Source: Internet
Author: User

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

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.