2016-04-29 95 views
-1

我有一個.NET應用程序,我已經完成並且正在客戶機器上運行。我得到報告,它偶爾會崩潰與Windows錯誤框發佈的.NET程序崩潰

xxxProgram已停止工作,一個問題導致程序停止正常工作。 Windows將關閉該程序並通知您是否有解決方案。

我想追蹤這個問題的根源。我認爲這是一個未處理的異常,但我無法找到位置。我正在尋找幫助來追蹤來源的輸入。雖然程序是.NET,但我使用OPC等一些非託管資源和我編寫的小包裝,但我覺得我很好地涵蓋了編組。該應用程序幾乎全天候運行,目前每4-10天可能會崩潰一次。

我在想的一件事是訂閱AppDomain.UnhandledExceptionEventHandler並將堆棧跟蹤記錄到文件中。這是可能的,以及如何去做這件事?

謝謝

+1

不是編程解決方案,但是從一個老鄉疑難解答評論 - 很多時候,與終止意外的應用程序,有相關記錄的東西Windows事件查看器,就像特定的堆棧跟蹤一樣。 – Jake

+0

爲什麼downvote ??? – MDK

回答

1

我會創建一個日誌系統。即使它只是一個嘗試從網頁中獲取基本錯誤。我以前使用以下代碼將日誌文件存儲在服務器上。

public void logError(string errorString) 
{ 
    try 
    { 
     // Takes unknown errors and logs them to the log file 
     string user = HttpContext.Current.User.Identity.Name.ToString(); 
     string FileLine = user + ", " + DateTime.Now.ToString() + ", " + errorString; 
     string filename = HttpContext.Current.Server.MapPath("../Log/error_log.txt"); 

     using (System.IO.StreamWriter file = new System.IO.StreamWriter(filename, true)) 
     { 
      file.WriteLine(FileLine); 
      file.WriteLine("---------------------------------------"); 
     } 
    } 
    catch (Exception ex) 
    { 
     // send email, or another notification 
    } 

} 

你可以把它想:

try 
{ 
    //function code 
} 
catch (Exception ex) 
{                     
    logError(ex.ToString()); 
}