Using filter to track ASP. NET MVC page load times

Source: Internet
Author: User
Tags log4net

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 classTrackpageloadperformanceattribute:actionfilterattribute {//Here we use log4net to print out the results.       Private Static ReadOnlyILog Log =Logmanager.getlogger (Methodbase.getcurrentmethod ().      DeclaringType); //Create a dictionary to record the start time, and key is the thread ID of the access.       Private ReadOnlydictionary<int, datetime> _start =Newdictionary<int, datetime>(); //Create a dictionary to record the currently visited page URL.       Private ReadOnlydictionary<int,string> _url =Newdictionary<int,string>();  Public Override voidonactionexecuting (ActionExecutingContext filtercontext) {//filter out Childaction, because childaction is actually not a separate page           if(filtercontext.ischildaction)return; varCurrentthreadid =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 voidonresultexecuted (ResultExecutedContext filtercontext) {varCurrentthreadid =System.Threading.Thread.CurrentThread.ManagedThreadId; if(!_start. ContainsKey (Currentthreadid))return; Try           {                //Calculate the current page access time               varCostseconds = (DateTime.Now-_start[currentthreadid]).               TotalSeconds; if(Costseconds >2)//if it takes more than 2 seconds, it is printed with log4net, which page is accessed more than 2 seconds, and how long it takes. {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 filter to track ASP. NET MVC page load times

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.