C# ClrMD无法附加到生产服务器上的进程

C# ClrMD无法附加到生产服务器上的进程,c#,C#,我试图使用ClrMD转储在特定进程中运行的所有线程的堆栈跟踪。该代码在我的开发环境中运行良好,但在生产服务器上运行不好 服务器正在运行:Windows server 2012 R2标准版 我收到的错误是: 无法附加到进程。错误0 询问如何将ClrMD附加到另一个用户进程,这正是我试图做的。我终止了进程(作为windows服务运行),并以尝试执行ClrMD的同一用户的身份启动了该进程。我仍然得到错误 尝试给用户调试权限,但也没有帮助 我打赌问题与如何配置生产服务器有关。我有管理员权限 对下一步做什

我试图使用ClrMD转储在特定进程中运行的所有线程的堆栈跟踪。该代码在我的开发环境中运行良好,但在生产服务器上运行不好

服务器正在运行:Windows server 2012 R2标准版

我收到的错误是:

无法附加到进程。错误0

询问如何将ClrMD附加到另一个用户进程,这正是我试图做的。我终止了进程(作为windows服务运行),并以尝试执行ClrMD的同一用户的身份启动了该进程。我仍然得到错误

尝试给用户调试权限,但也没有帮助

我打赌问题与如何配置生产服务器有关。我有管理员权限

对下一步做什么有什么建议吗

代码:

使用系统;
使用System.Collections.Generic;
使用系统诊断;
使用System.Linq;
使用Microsoft.Diagnostics.Runtime;
命名空间控制台应用程序4
{
班级计划
{
静态void Main(字符串[]参数)
{
int-pid=0;
var result=newdictionary();
var targetProcessName=“Dist.TingbogScraper.Business.TingbogScraperService.vshost”;
//将此更改为您正在寻找的流程
var outputPath=“C:\\temp\\ClrMDresult.txt”;
var exceptionOutput=“C:\\temp\\ClrMDdump.txt”;
var processs=Process.getprocesss();
foreach(过程中的var过程)
{
if(process.ProcessName.Contains(targetProcessName))
{
pid=进程Id;
}
}
尝试
{
使用(var dataTarget=dataTarget.AttachToProcess(pid,5000,AttachFlag.Passive))
{
ClrRuntime runtime=dataTarget.ClrVersions.First().CreateRuntime();
foreach(runtime.Threads中的var t)
{
尝试
{
if(t.StackTrace!=null)
{
结果。添加(
t、 ManagedThreadId,
t、 StackTrace.Select(f=>
{
if(f.Method!=null)
{
返回f.Method.Type.Name+“+f.Method.Name;
}
返回null;
}).ToArray()
);
}
}
捕获(例外情况除外)
{
}
}
}
foreach(结果中的var kvp)
{
var值=kvp.值;
foreach(var stacktrace值)
{
System.IO.File.AppendAllText(outputPath,
Format(“{0}{1}{2}”,kvp.Key,stacktrace,Environment.NewLine));
}
}
}
捕获(ClrDiagnosticsException ex)
{
System.IO.File.AppendAllText(outputPath,
格式(“{0}{1}{2}”,例如Message、ex.StackTrace、ex.Source));
}
}
}
}

发现我的开发环境中的流程名称与生产环境中的流程名称不同

更正进程名称修复了错误

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));
            }
        }
    }
}