如何在代码中获取线程“ Location” / StackTrace?

时间:2019-01-07 23:10:00

标签: c# wpf multithreading clrmd

我目前有以下代码(使用CLRMD)试图获取堆栈跟踪:

var pid = Process.GetCurrentProcess().Id;
using (var dataTarget = DataTarget.AttachToProcess(pid, 5000, AttachFlag.Passive))
    {
        ClrInfo currentRuntime = dataTarget.ClrVersions[0];
        var runtime = currentRuntime.CreateRuntime();

        Debug.Print("Stack Traces:");
        foreach (var t in runtime.Threads)
        {
            if (t.ManagedThreadId == 1)
            {
                foreach(var f in t.EnumerateStackTrace())
                {
                    Debug.Print(f.Method?.GetFullSignature());
                }
            }
        }
        Debug.Print("");
    }

它打印出:

System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame)
System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame)
System.Windows.Application.RunDispatcher(System.Object)
System.Windows.Application.RunInternal(System.Windows.Window)
System.Windows.Application.Run(System.Windows.Window)
System.Windows.Application.Run()
App.App.Main(System.String[])

但是当我在Visual Studio中的调试下打开threads标签时,我看到了更具描述性的Location标签,其中包含以下内容:

App.exe!App.ViewModels.SomethingSomethingViewModel.SomethingHandler
App.exe!App.ViewModels.SomethingSomethingParentViewModel.SomethingCommandHandler() Line 110
App.exe!App.Utilities.AsyncRelayCommand.ExecuteAsync(object parameter) Line 55...

如何获取带有位置选项卡信息的stacktrace? Visual Studio完美地显示了它,但是我似乎找不到如何通过代码访问它的方法,因此我可以将它作为日志发送出去。

0 个答案:

没有答案