我试图使用ClrMD来转储在特定进程中运行的所有线程的堆栈跟踪。代码在我的开发环境中运行得很好,但在生产服务器上却不行。
服务器正在运行: Windows 2012 R2标准
我收到的错误是:
无法附加到进程。错误0。
This post询问如何将ClrMD附加到另一个用户进程,这正是我想要做的。我终止了进程(它作为一个windows服务运行),并将它作为我试图与之执行ClrMD的同一个用户启动。我还是会犯错误。
尝试让用户调试私有程序,但这也没有帮助。
我打赌这个问题与如何配置生产服务器有关。我有管理员的权利。
对下一步该做什么有什么建议吗?

代码:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.Diagnostics.Runtime;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
int pid = 0;
var result = new Dictionary<int, string[]>();
var targetProcessName = "Dist.TingbogScraper.Business.TingbogScraperService.vshost";
// Change this to the process you are looking for
var outputPath = "C:\\temp\\ClrMDresult.txt";
var exceptionOutput = "C:\\temp\\ClrMDdump.txt";
var processes = Process.GetProcesses();
foreach (var process in processes)
{
if (process.ProcessName.Contains(targetProcessName))
{
pid = process.Id;
}
}
try
{
using (var dataTarget = DataTarget.AttachToProcess(pid, 5000, AttachFlag.Passive))
{
ClrRuntime runtime = dataTarget.ClrVersions.First().CreateRuntime();
foreach (var t in runtime.Threads)
{
try
{
if (t.StackTrace != null)
{
result.Add(
t.ManagedThreadId,
t.StackTrace.Select(f =>
{
if (f.Method != null)
{
return f.Method.Type.Name + "." + f.Method.Name;
}
return null;
}).ToArray()
);
}
}
catch (Exception ex)
{
}
}
}
foreach (var kvp in result)
{
var value = kvp.Value;
foreach (var stacktrace in value)
{
System.IO.File.AppendAllText(outputPath,
string.Format("{0} {1} {2}", kvp.Key, stacktrace, Environment.NewLine));
}
}
}
catch (ClrDiagnosticsException ex)
{
System.IO.File.AppendAllText(outputPath,
string.Format("{0} {1} {2}", ex.Message, ex.StackTrace, ex.Source));
}
}
}
}发布于 2016-12-02 10:31:16
发现与生产相比,在我的开发环境中流程的名称是不同的。
纠正进程的名称,修正错误。
https://stackoverflow.com/questions/40929833
复制相似问题