2016-12-13 73 views
-1

我需要編寫一個工具來監聽某個目標進程(在客戶環境中崩潰),並且一旦崩潰,它應該通過啓動DebugDiag來傳遞命令行參數來生成轉儲。代碼需要用C#編寫。我已經做了一些編碼,但該工具從未檢測到啓動的過程。 下面是代碼:轉儲崩潰轉儲

static void Main(string[] args) 
{ 
    ManagementEventWatcher startWatch = new ManagementEventWatcher(
    new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace")); 
    startWatch.EventArrived 
         += new EventArrivedEventHandler(startWatch_EventArrived); 
    startWatch.Start(); 

    Console.WriteLine("Press ENTER to exit"); 
    Console.ReadLine(); 
    startWatch.Stop(); 

} 


    static void startWatch_EventArrived(object sender, EventArrivedEventArgs e) 
    { 
     string name = e.NewEvent.Properties["ProcessName"].Value as string; 
     Console.WriteLine("Process started: {0}", name); 
     if (name != null && name.Contains("My Process.exe")) 
     { 
      string procpath = "C:\\Program Files\\DebugDiag"; 
      string filename = Path.Combine(procpath, "DbgHost.exe"); 
      var proc = System.Diagnostics.Process.Start(filename, "-dump My Process.exe"); 
     } 
    } 

也請告知,如果這是通過命令行參數DebugDiag資料 @Bruno,我使用ProcDump實現你的建議的方式。現在它工作一次,這意味着當我啓動我的目標進程(32位)時,ProcDump也會啓動,但是我的應用程序是這樣的,當我啓動一個工作區時,它啓動另一個具有相同名稱的進程,此時ProcDump失敗,在調試我發現,它拋出一個異常說,32位進程無法調試64位進程和我所有的目標進程只有32位... 代碼:

static void startWatch_EventArrived(object sender, EventArrivedEventArgs e) 
{ 
    string name = e.NewEvent.Properties["ProcessName"].Value as string; 
    Console.WriteLine("Process started: {0}", name); 
    if (name != null && name.Contains("MyProcess.exe")) 
    { 
     string procpath = "C:\\Procdump"; 
     string filename = Path.Combine(procpath, "procdump.exe"); 
     var proc = System.Diagnostics.Process.Start(filename, "-e -f -mp -n 25 -w -accepteula MyProcess.exe MyProcess_crash"); 
    } 
} 
+0

Windows和DebugDiag資料已經內置支持此() - 爲什麼另起爐竈? – Polyfun

+0

我知道有一個內置的支持。不過,我不能指望我的客戶做配置。相反,他們應該能夠啓動一個簡單的.exe(即我的代碼)並執行必要的操作。這不是降級我的查詢的確切原因。我不打算重新發明任何車輪。 – Sandeep

回答