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