"Problems encountered at work" Dictionary an add operation to make the report go beyond the bounds of the array

Source: Internet
Author: User

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

Related Article

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.