我正在使用Microsoft.Diagnostics.Runtime nuget包,这是我尝试获取堆栈跟踪时的代码:
var pid = Process.GetCurrentProcess().Id;
// Line of error
using (var dataTarget = DataTarget.AttachToProcess(pid, 5000, AttachFlag.Invasive))
{
ClrInfo currentRuntime = dataTarget.ClrVersions[0];
var runtime = currentRuntime.CreateRuntime();
foreach (var t in runtime.Threads)
{
MessageBox.Show("Got here");
t.StackTrace
}
}问题类似于Attach to self with ClrMD? HRESULT: 0x80070057,但我更进一步,使用Wix构建应用程序。然后,我将应用程序安装到我的桌面上,这样它就可以在没有Visual Studio及其调试器的情况下运行。
只要我将其放在using (var dataTarget = DataTarget.AttachToProcess(pid, 5000, AttachFlag.Invasive))行之后,消息框就不会显示。如果我把messagebox放在前面,messagebox就会出现。
在代码中,我得到了错误
Microsoft.Diagnostics.Runtime.ClrDiagnosticsException: 'Could not attach to pid 624, HRESULT: 0x80070057'我想我明白为什么当我在Visual Studio中运行应用程序时AttachFlag.Invasive不能工作,因为它正在被调试,但我不明白为什么在我用Wix构建它并将它安装在我的桌面上之后,这行代码不能工作。
同样,与附加的Stackoverflow帖子一样,AttachFlag.Invasive和AttachFlag.NonInvasive不能工作,但AttachFlag.Passive可以工作。
发布于 2019-09-05 18:30:01
就像我在下面的回答:https://stackoverflow.com/a/57790758/7122901
您可以使用DataTarget.CreateSnapshotAndAttach。此方法创建流程的快照,并从该快照创建DataTarget。示例:
var processId = Process.GetCurrentProcess().Id;
using (var dataTarget = DataTarget.CreateSnapshotAndAttach(processId))
{
}https://stackoverflow.com/questions/54101095
复制相似问题