private static void BuildCallTree(Stream stream) { List <AsyncCallTrackingTraceListener.AsyncIteratorState> asyncIteratorStates; AsyncCallTrackingTraceListener.AsyncIteratorState asyncIteratorState; List <AsyncCallTrackingTraceListener.AsyncIteratorState> asyncIteratorStates1 = new List <AsyncCallTrackingTraceListener.AsyncIteratorState>(); Dictionary <AsyncCallTrackingTraceListener.AsyncIteratorState, List <AsyncCallTrackingTraceListener.AsyncIteratorState> > asyncIteratorStates2 = new Dictionary <AsyncCallTrackingTraceListener.AsyncIteratorState, List <AsyncCallTrackingTraceListener.AsyncIteratorState> >(); lock (AsyncCallTrackingTraceListener.iterators) { DateTime utcNow = DateTime.UtcNow; foreach (AsyncCallTrackingTraceListener.AsyncIteratorState value in AsyncCallTrackingTraceListener.iterators.Values) { AsyncCallTracker caller = value.Iterator.Caller; if (caller != null) { if (!AsyncCallTrackingTraceListener.iterators.TryGetValue(caller.Caller, out asyncIteratorState)) { continue; } if (!asyncIteratorStates2.TryGetValue(asyncIteratorState, out asyncIteratorStates)) { asyncIteratorStates = new List <AsyncCallTrackingTraceListener.AsyncIteratorState>(1); asyncIteratorStates2.Add(asyncIteratorState, asyncIteratorStates); } asyncIteratorStates.Add(value); } else { asyncIteratorStates1.Add(value); } } using (XmlTextWriter xmlTextWriter = new XmlTextWriter(stream, Encoding.UTF8)) { xmlTextWriter.Formatting = Formatting.Indented; xmlTextWriter.WriteStartDocument(); xmlTextWriter.WriteStartElement("callTree"); xmlTextWriter.WriteStartAttribute("time"); xmlTextWriter.WriteValue(utcNow); xmlTextWriter.WriteEndAttribute(); foreach (AsyncCallTrackingTraceListener.AsyncIteratorState asyncIteratorState1 in asyncIteratorStates1) { AsyncCallTrackingTraceListener.AppendCallTree(utcNow, xmlTextWriter, asyncIteratorState1, asyncIteratorStates2); } xmlTextWriter.WriteEndElement(); xmlTextWriter.WriteEndDocument(); } } }
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(); }
public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data) { AsyncCallTrackingTraceListener.StaticTraceData(eventCache, source, eventType, data); }