Using the filter to track ASP. NET MVC page load

Source: Internet
Author: User
Tags httpcontext log4net

Reprint Address: http://www.cnblogs.com/JustRun1983/p/4027929.html

Recently, customers have been feedback system use slow, sometimes can point out which page, and sometimes just general feedback slow. This kind of problem is like a ghost, very difficult to deal with. Because of the many factors that lead to this problem, it is difficult to simulate the actual running environment in the development project. In theory, stress testing is a solution for all pages, but the cost of this approach is too high and there is no way to quickly locate and solve customer problems.

Finally, consider creating a filter to record a time before accessing the action, and then recording the time after the page render finishes. Track the load performance of each page by comparing the 2-time difference. If you are not familiar with the filter in MVC, you can see here ASP. NET MVC uses filter to dismiss the session, cookie and so on, the article has the introduction and use of filter.

Go straight to the code. Override the onactionexecuting and onresultexecuted methods, respectively, by inheriting ActionFilterAttribute.

public class Trackpageloadperformanceattribute:actionfilterattribute {//here use Log4net to print out the result private static R Eadonly ILog Log = Logmanager.getlogger (Methodbase.getcurrentmethod ().      DeclaringType);       Create a dictionary to record the start time, and key is the thread ID of the access.      Private ReadOnly dictionary<int, datetime> _start = new Dictionary<int, datetime> ();       Create a dictionary to record the currently visited page URL.       Private readonly Dictionary<int, string > _url = new Dictionary<int, string> (); public override void OnActionExecuting (ActionExecutingContext filtercontext) {//filter out childaction, because CHILDAC           tion is actually not a separate page if (filtercontext.ischildaction) return;           var currentthreadid = System.Threading.Thread.CurrentThread.ManagedThreadId; try {_start.               ADD (Currentthreadid, DateTime.Now); _url. ADD (currentthreadid, FILTERCONTEXT.HTTPCONTEXT.REQUEST.URL = = null? string. Empty:filterContext.HttpContext.ReqUest.           Url.absoluteuri); } catch (Exception ex) {Log.error (ex.           ToString ()); }} public override void Onresultexecuted (ResultExecutedContext filtercontext) {var currentth           Readid = System.Threading.Thread.CurrentThread.ManagedThreadId; if (!_start.           ContainsKey (Currentthreadid)) return; try {//calculates the current page access time-consuming var costseconds = (DateTime.Now-_start[currentthreadid]).               TotalSeconds;               if (Costseconds > 2)//If it takes more than 2 seconds, it is printed with log4net, specifically which page access is more than 2 seconds, the specific use of how long. {Log.info (string. Format ("Access the action more than 2 seconds.  Cost seconds {1}.               URL: {0} ", _url[currentthreadid], costseconds)); }} catch (Exception ex) {Log.error (ex).           ToString ()); } finally {_start.               Remove (Currentthreadid); _url. Remove (CurrenTthreadid); }       }   }

Finally, the filter is registered as the global filter so that it can monitor the load time of all pages in the system.

If you have a similar performance problem with me, try it, plus this filter, which page in the online system is slow, you can see it at a glance.
About ASP. NET MVC performance monitoring, and this article may be able to help you. Using Miniprofiler for ASP. NET MVC and Entity Framework pulse (with source)

Using the filter to track ASP. NET MVC page load (GO)

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.