microsoft / perfview

PerfView is a CPU and memory performance-analysis tool
http://channel9.msdn.com/Series/PerfView-Tutorial
MIT License
4.06k stars 696 forks source link

Doc: how to get event call stack from .nettrace events #1959

Open verdie-g opened 7 months ago

verdie-g commented 7 months ago

I'm a bit confused about how to read events from a .nettrace file. I think I'm supposed to use the EventPipeEventSource with such code

using EventPipeEventSource source = new(nettraceFileStream);
source.Clr.All += evt => evt.CallStack();
source.Process();

but TraceEvent.CallStack throws with Attempted to use TraceLog support on a non-TraceLog TraceEventSource so now it seems like I'm not using the right event source. What is the right API to use?

brianrob commented 6 months ago

The issue that you're running into here is that the CallStack method can only be used on events that come from a TraceLogEventSource. You will need to create a TraceLog, and then you can call TraceLog.Events.GetSource(), register the callbacks, and then call Process on the source.

string etlxPath = TraceLog.CreateFromEventPipeDataFile(pathToNetTrace);
using TraceLog traceLog = new TraceLog(etlxPath);
using TraceLogEventSource source = traceLog.Events.GetSource();
source.Clr.All += evt => evt.CallStack();
source.Process();