代码之家  ›  专栏  ›  技术社区  ›  Thomas

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

  •  4
  • Thomas  · 技术社区  · 7 年前

    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[])
    

    但是当我打开门的时候 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 回复  |  直到 7 年前