Example #1
0
        public void StartTrace()
        {
            startTime = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
            if (startTime < threadStartTime)
            {
                threadStartTime = startTime;
            }
            if (Thread.CurrentThread.ManagedThreadId != currentThreadId)
            {
                if (threadsHelpInfo.ContainsKey(Thread.CurrentThread.ManagedThreadId))
                {
                    threadsHelpInfo[currentThreadId] = new ThreadHelpInfo(methodsLists,
                                                                          currentMethodList, startTimes, threadStartTime, isStarted);
                    currentThreadId = Thread.CurrentThread.ManagedThreadId;
                    ThreadHelpInfo currentThreadInfo = threadsHelpInfo[currentThreadId];
                    methodsLists      = currentThreadInfo.methodsLists;
                    currentMethodList = currentThreadInfo.currentMethodList;
                    startTimes        = currentThreadInfo.startTimes;
                    isStarted         = currentThreadInfo.isStarted;
                    threadStartTime   = currentThreadInfo.threadStartTime;
                }
                else
                {
                    threadsHelpInfo[currentThreadId] = new ThreadHelpInfo(methodsLists,
                                                                          currentMethodList, startTimes, threadStartTime, isStarted);
                    currentThreadId = Thread.CurrentThread.ManagedThreadId;

                    //Initializing new thread
                    threadInfo = new ThreadRuntimeInfo()
                    {
                        Id           = currentThreadId,
                        EllapsedTime = 0, Methods = new List <MethodRuntimeInfo>()
                    };
                    currentMethodList = threadInfo.Methods;
                    methodsLists      = new Stack <List <MethodRuntimeInfo> >();
                    methodsLists.Push(currentMethodList);
                    isStarted = false;
                    result.Threads.Add(threadInfo);
                    threadStartTime = startTime;
                    //-------------------------------

                    //constructor parameters does not matter
                    threadsHelpInfo.Add(currentThreadId, new ThreadHelpInfo(methodsLists,
                                                                            currentMethodList, startTimes, threadStartTime, isStarted));
                }
            }
            if (isStarted)
            {
                startTimes.Push(startTime);
                methodsLists.Push(currentMethodList);
                currentMethodList.Add(new MethodRuntimeInfo());
                currentMethodList = currentMethodList[currentMethodList.Count - 1].Methods;
            }
            isStarted = true;
        }
Example #2
0
        public void StopTrace()
        {
            StackFrame frame      = new StackFrame(1);
            var        method     = frame.GetMethod();
            string     methodName = method.Name;
            string     className  = method.DeclaringType.Name;

            endTime = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
            if (Thread.CurrentThread.ManagedThreadId != currentThreadId)
            {
                threadsHelpInfo[currentThreadId] = new ThreadHelpInfo(methodsLists,
                                                                      currentMethodList, startTimes, threadStartTime, isStarted);
                currentThreadId = Thread.CurrentThread.ManagedThreadId;
                ThreadHelpInfo currentThreadInfo = threadsHelpInfo[currentThreadId];
                methodsLists      = currentThreadInfo.methodsLists;
                currentMethodList = currentThreadInfo.currentMethodList;
                startTimes        = currentThreadInfo.startTimes;
                isStarted         = currentThreadInfo.isStarted;
                threadStartTime   = currentThreadInfo.threadStartTime;
            }
            if (!isStarted)
            {
                startTime         = startTimes.Pop();
                currentMethodList = methodsLists.Pop();
                currentMethodList[currentMethodList.Count - 1].EllapsedTime = endTime - startTime;
                currentMethodList[currentMethodList.Count - 1].ClassName    = className;
                currentMethodList[currentMethodList.Count - 1].MethodName   = methodName;
            }
            else
            {
                currentMethodList.Add(new MethodRuntimeInfo()
                {
                    EllapsedTime =
                        endTime - startTime,
                    ClassName  = className,
                    MethodName = methodName
                });
                isStarted = false;
            }
            threadInfo.EllapsedTime = endTime - threadStartTime;
        }