3

我目前有这段代码(使用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...

如何获取带有位置选项卡信息的堆栈跟踪?Visual Studio 完美地显示了它,但我似乎无法找到如何通过代码访问它,所以我可以将它作为日志发送......

4

0 回答 0