This project has moved. For the latest updates, please go here.

How to get call stack of an exception thrown event

Feb 9, 2011 at 7:01 AM

I use TraceEvents class to capture all clr exception events :

namespace ExpMon
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        private const string _SessionName = "ExpMon";
        private readonly TraceEventSession _Session;
        private readonly ETWTraceEventSource _Source;
        private readonly ObservableCollection<string> _Logs = new ObservableCollection<string>();
        private bool _IsRecording = false;

        public MainWindow()
            this.lstLogs.ItemsSource = _Logs;

            var sessions = TraceEventSession.GetActiveSessionNames().ToList();
            if (sessions.Contains(_SessionName))
                _Session = new TraceEventSession(_SessionName) { StopOnDispose = true };
                _Session = new TraceEventSession(_SessionName, null) { StopOnDispose = false };
                _Session.EnableProvider(ClrTraceEventParser.ProviderGuid, TraceEventLevel.Informational);

            _Source = new ETWTraceEventSource(_SessionName, TraceEventSourceType.Session);
            _Source.Clr.ExceptionStart += Clr_ExceptionStart;
            _Source.Clr.ClrStackWalk += new Action<ClrStackWalkTraceData>(Clr_ClrStackWalk);
            WaitCallback proc = o => _Source.Process();

        void Clr_ClrStackWalk(ClrStackWalkTraceData obj)


        void Clr_ExceptionStart(ExceptionTraceData obj)
            var info = obj.ToString();
            Action log = () => _Logs.Add(info);

        private void btnStart_Click(object sender, RoutedEventArgs e)
            _IsRecording = true;

But I'm  unable to get callstack for an exception, and I saw this:

The stack event should be used in conjunction with other events to generate stack traces after an event is raised. It is logged when the runtime provider is enabled. This is a very high frequency event, because it is raised whenever another runtime event is raised. For this reason, we recommend that you use this event with caution.

But how to use this event to get the callstack of an exception thrown, any idea? The document is not very clear about this.
Thanks in advance.
May 31, 2011 at 4:46 PM

This is a late reply.

What you did looks like it should work, but I don't have personal experience trying to do it.    I do know that this will NOT work unless the version of the CLR is at at least V4.0.   Certainly check that first.   If it still does not work, I can see if I can dig up more information

Jun 1, 2011 at 2:04 AM

OK, thanks so much.

If my code should work, could you please help me to debug it? Thanks.

Jun 1, 2011 at 5:19 PM

It will be  a few days before I can look at this.   I will report back early next week.