我正在使用 CLR 内存诊断库来获取正在运行的进程中所有线程的堆栈跟踪:
var result = new Dictionary<int, string[]>();
var pid = Process.GetCurrentProcess().Id;
using (var dataTarget = DataTarget.AttachToProcess(pid, 5000, AttachFlag.Passive))
{
string dacLocation = dataTarget.ClrVersions[0].TryGetDacLocation();
var runtime = dataTarget.CreateRuntime(dacLocation); //throws exception
foreach (var t in runtime.Threads)
{
result.Add(
t.ManagedThreadId,
t.StackTrace.Select(f =>
{
if (f.Method != null)
{
return f.Method.Type.Name + "." + f.Method.Name;
}
return null;
}).ToArray()
);
}
}
我从这里得到了这段代码,它似乎对其他人有用,但它在指示的行上为我抛出了一个异常,并带有消息This runtime is not initialized and contains no data.
dacLocation
设置为C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\mscordacwks.dll