The main function of the problem code is to record the execution time of the page:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSYSTEM.WEB.MVC;6 usingtcwireless.utility;7 usingSystem.Threading;8 usingTCBase.EagleEye.Metric;9 Ten namespaceTCWireless.Controller.BaseController One { A Public classTimefilter:actionfilterattribute - { - Public stringName {Get;Set; } the //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>(); - Public Override voidonactionexecuting (actionexecutingcontext filtercontext) - { + //filter out Childaction, because childaction is actually not a separate page - if(filtercontext.ischildaction)return; + varCurrentthreadid =Thread.CurrentThread.ManagedThreadId; A Try at { - if(!_start. ContainsKey (Currentthreadid)) - { - _start. ADD (Currentthreadid, datetime.now); - } - } in Catch(Exception ex) - { toLogwrapper.addinfo ("Mobilehotel","Timefilter","onactionexecuting", ex); + } - } the Public Override voidonresultexecuted (resultexecutedcontext filtercontext) * { $ varCurrentthreadid =Thread.CurrentThread.ManagedThreadId;Panax Notoginseng if(!_start. ContainsKey (Currentthreadid))return; - Try the { + //Calculate the current page access time A varCostseconds = (DateTime.Now-_start[currentthreadid]). TotalMilliseconds; the intValue =Convert.ToInt32 (costseconds); + MetricClient.Instance.LogTiming (Name, value); - } $ Catch(Exception ex) $ { -Logwrapper.addinfo ("Mobilehotel","Timefilter","onresultexecuted", ex); - } the finally - {Wuyi _start. Remove (currentthreadid); the } - } Wu } -}
The problem appears on line 26th.
Then Baidu found other people also found such problems, because dictionnary is multithreaded insecure. Need to add a lock
"Problems encountered at work" Dictionary an add operation to make the report go beyond the bounds of the array