ASP. NET simple record the client and server processing time for the request

Source: Internet
Author: User

Recent projects need a simple record of AJAX client and server processing time, the idea of server time is to borrow the beginrequest and endrequest events, in order not to affect the data format returned by the existing interface, so the service processing time is placed in the header of response.

BeginRequest + = (sender, args) ={httpcontext.current.items["Serverstarttime"] =DateTime.Now.Ticks.ToString ();            }; EndRequest+ = (sender, args) = = {                LongTicks = Convertutil.tolong (httpcontext.current.items["Serverstarttime"],0); TimeSpan TS=NewTimeSpan (DateTime.Now.Ticks-ticks); HttpContext.Current.Response.Headers.Set ("Serverprocesstime", math.ceiling (ts. TotalMilliseconds).            ToString ()); };

With the Beforesend and complete events, the client implements code as follows:

$(["Get","Post"]). Each (function (i, Ajaxtype) {G[ajaxtype]=function (option) {$.ajax ({url:option.url, data:option.data, Type:ajaxtype, Beforesend:function () {option. Startclienttime=NewDate (); }, DataType:"JSON", Cache:option.cache,Async: option.Async, Success:function (d) {alert (d)}, CO Mplete:function (XMLHttpRequest, textstatus) {varEndclienttime =NewDate (); Try {                        varLogrequest =false; if(Option.url.indexOf ("/member/aaa") >=0||Option.url.indexOf ("/member/bbb") >=0||Option.url.indexOf ("/MEMBER/CCC") >=0) {logrequest=true; }                        if(logrequest) {//Record Request Time                            varCosttime = endclienttime-(option. Startclienttime-0); varServertime = Xmlhttprequest.getresponseheader ("Serverprocesstime"); Console.log ("rquest"+ Option.url +"Totlal Cost Time:"+ Costtime +"Server Time:"+servertime); varLogdata ={ajaxUrl:option.url, totalticks:costtime,                                                      Serverticks:servertime}; $.post ("/xxx/logrequest", logdata);                        }                    } Catch(e) {}}});    }; });

Finally, the time is recorded in the log system by calling Logrequest.

ASP. NET simple record the client and server processing time for the request

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.