2013-04-05 67 views
17

我有這樣的代碼:Global.asax - Application_Error - 如何獲取頁面數據?

using System.Configuration; 

void Application_Error(object sender, EventArgs e) 
{ 
    Exception ex = Server.GetLastError().GetBaseException(); 

    string ErrorMessage = ex.Message; 
    string StackTrace = ex.StackTrace; 
    string ExceptionType = ex.GetType().FullName; 
    string UserId = Getloggedinuser(); 
    string WebErrorSendEmail = 
     ConfigurationManager.AppSettings["WebErrorSendEmail"]; 

    // save the exception in DB 
    LogStuffInDbAndSendEmailFromDb(); 
} 

這是(最),我的代碼。 在一小部分病例中,我沒有收到足夠的信息。我不知道異常起源於哪個頁面。

我怎樣才能獲得與異常源自的頁面相關的任何信息?

下面是最短的消息的示例:

用於基地-64字符數組無效長度。

在System.Convert.FromBase64String(字符串或多個)處 System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize System.Web.UI.ObjectStateFormatter.Deserialize(字符串inputString)(字符串 serializedState)在 System.Web.UI.Util.DeserializeWithAssert(IStateFormatter格式化, 字符串serializedState)在 System.Web.UI.HiddenFieldPageStatePersister.Load()

回答

23

你可以得到當前請求的URL和頁面像這樣:

void Application_Error(object sender, EventArgs e) 
{ 
    // Code that runs when an unhandled error occurs 
    if (HttpContext.Current != null) 
    { 
     var url = HttpContext.Current.Request.Url; 
     var page = HttpContext.Current.Handler as System.Web.UI.Page; 
    } 
} 
相關問題