コード例 #1
0
        public static void StaticTraceData(TraceEventCache eventCache, string source, TraceEventType eventType, object data)
        {
            AsyncCallTrackingTraceListener.AsyncIteratorState asyncIteratorState;
            if (source != "AsyncHelper")
            {
                return;
            }
            AsyncIteratorContextBase asyncIteratorContextBase = data as AsyncIteratorContextBase;
            TraceEventType           traceEventType           = eventType;

            if (traceEventType <= TraceEventType.Stop)
            {
                if (traceEventType == TraceEventType.Start)
                {
                    lock (AsyncCallTrackingTraceListener.iterators)
                    {
                        asyncIteratorState = new AsyncCallTrackingTraceListener.AsyncIteratorState()
                        {
                            Iterator = asyncIteratorContextBase,
                            ThreadId = eventCache.ThreadId
                        };
                        AsyncCallTrackingTraceListener.iterators.Add(asyncIteratorState.Iterator, asyncIteratorState);
                    }
                }
                else
                {
                    if (traceEventType != TraceEventType.Stop)
                    {
                        return;
                    }
                    lock (AsyncCallTrackingTraceListener.iterators)
                    {
                        AsyncCallTrackingTraceListener.iterators.Remove(asyncIteratorContextBase);
                    }
                }
            }
            else if (traceEventType == TraceEventType.Suspend)
            {
                lock (AsyncCallTrackingTraceListener.iterators)
                {
                    AsyncCallTracker asyncCallTracker = (AsyncCallTracker)data;
                    asyncIteratorState               = AsyncCallTrackingTraceListener.iterators[asyncCallTracker.Caller];
                    asyncIteratorState.Call          = asyncCallTracker;
                    asyncIteratorState.SuspendedTime = DateTime.UtcNow;
                }
            }
            else
            {
                if (traceEventType != TraceEventType.Resume)
                {
                    return;
                }
                lock (AsyncCallTrackingTraceListener.iterators)
                {
                    asyncIteratorState          = AsyncCallTrackingTraceListener.iterators[asyncIteratorContextBase];
                    asyncIteratorState.Call     = null;
                    asyncIteratorState.ThreadId = eventCache.ThreadId;
                }
            }
        }
コード例 #2
0
        private static void AppendCallTree(DateTime now, XmlTextWriter writer, AsyncCallTrackingTraceListener.AsyncIteratorState methodState, Dictionary <AsyncCallTrackingTraceListener.AsyncIteratorState, List <AsyncCallTrackingTraceListener.AsyncIteratorState> > allCalls)
        {
            List <AsyncCallTrackingTraceListener.AsyncIteratorState> asyncIteratorStates;

            writer.WriteStartElement("method");
            writer.WriteStartAttribute("name");
            writer.WriteString(methodState.Iterator.MethodName);
            writer.WriteEndAttribute();
            if (methodState.Call != null)
            {
                TimeSpan timeSpan = now - methodState.SuspendedTime;
                writer.WriteStartAttribute("suspendedMilliseconds");
                writer.WriteValue(timeSpan.TotalMilliseconds);
                writer.WriteEndAttribute();
            }
            else
            {
                writer.WriteStartAttribute("runningOnThread");
                writer.WriteString(methodState.ThreadId);
                writer.WriteEndAttribute();
            }
            if (allCalls.TryGetValue(methodState, out asyncIteratorStates))
            {
                foreach (AsyncCallTrackingTraceListener.AsyncIteratorState asyncIteratorState in asyncIteratorStates)
                {
                    writer.WriteStartElement("call");
                    writer.WriteStartAttribute("name");
                    writer.WriteString(asyncIteratorState.Iterator.Caller.CalleeName);
                    writer.WriteEndAttribute();
                    if (object.ReferenceEquals(asyncIteratorState.Iterator.Caller, methodState.Call))
                    {
                        writer.WriteStartAttribute("waiting");
                        writer.WriteValue(true);
                        writer.WriteEndAttribute();
                    }
                    AsyncCallTrackingTraceListener.AppendCallTree(now, writer, asyncIteratorState, allCalls);
                    writer.WriteEndElement();
                }
            }
            writer.WriteEndElement();
        }