2016-11-13 25 views
0

我想在Catch {}中放置一個方法,將錯誤發送到我的「.log」文件。 我喜歡把頁面的地址和發生錯誤的方法。所以我希望你能幫助我,告訴我如何定義「strErrorPath」並將其作爲我的方法輸入。找到在C#頁面發生錯誤的地方的路徑

public static void SendErrorToPersonalErrorMessage(Exception ex,string strErrorPath) 
{ 
    string strErrorMEssage = string.Format("Error:{0} Time:{1:yyy/mm/dd - hh:mm:ss},Address:{2}", ex.Message, System.DateTime.Now, strErrorPath); 
    System.IO.StreamWriter oStreamWriter = null; 
    string strPersonalErrorMEssagePath = "~/App_Data/Log/PersonalErrorMessage.log"; 
    string strPersonalErrorMessagePathName = HttpContext.Current.Server.MapPath(strPersonalErrorMEssagePath); 
    oStreamWriter = new System.IO.StreamWriter(strPersonalErrorMessagePathName, true, System.Text.Encoding.UTF8); 
}`enter code here` 
+0

你在靶向:的WinForms,WPF,ASP ..? __Always__正確標記您的問題! – TaW

+0

我希望能找出在命名空間的c#類中會發生的那些錯誤:ASP.NET Web Form的工具。這很重要嗎? – Keyvan

回答

0

只需使用ex.Sourceex.StackTrace他們將展示有關異常的有用信息。

你可以閱讀更多關於StackTraceHere

+0

不客氣,兄弟。這是你在找什麼。或者是其他東西? – Hadi

0

另一種選擇是使用主叫信息屬性。 這裏是參考:MSDN : Caller Information (C#)

public static void Log(string errMsg, 
    [System.Runtime.CompilerServices.CallerMemberName] string member = "", 
    [System.Runtime.CompilerServices.CallerFilePath] string filePath = "", 
    [System.Runtime.CompilerServices.CallerLineNumber] int sourceLine = 0) 
    { 
     //log somthing... 
    } 
0

也許你可以試試:

 try 
     { 
      // your code... 
     } 
     catch (Exception e) 
     { 
      string strErrorPath = Request.Url.PathAndQuery; 
      SendErrorToPersonalErrorMessage(e, strErrorPath); 
     }