2010-07-02 75 views
0

我只是試圖在錯誤頁面上輸出應用程序錯誤的詳細信息。在global.asax我有一些處理,然後一個server.transfer到我的網頁與此代碼:HttpContext.Current.Server.GetLastError在IIS 7中不工作

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    'Get exception details 
    Dim ex As Exception = HttpContext.Current.Server.GetLastError() 

    If TypeOf ex Is HttpUnhandledException AndAlso ex.InnerException IsNot Nothing Then 
     ex = ex.InnerException 
    End If 

    If ex Is Nothing Then 
     lblError.Text = "<p>An unknown error occurred.</p>" 
    Else 
     'Determine what type of error we're dealing with 
     If TypeOf ex Is System.Data.SqlClient.SqlException Then 
      'A database-related exception... 
      lblError.Text = String.Format("<p>The database is having problems... please try your submission again later.<br />{0}</p>", ex.Message) 
     ElseIf TypeOf ex Is ApplicationException Then 
      'An ApplicationException has occurred 
      lblError.Text = String.Concat("<p>An application fault has been tripped:<br /> {0}</p>", ex.Message) 
     Else 
      'For all others, display the StackTrace 
      lblError.Text = String.Format("<p>An unknown error occurred. The following dump of exception information can be used to diagnose the issue</p><p class=""stackTrace""><strong>Exception:</strong> {0}</p><p class=""stackTrace""><strong>Message:</strong> {1}</p><p class=""stackTrace""><strong>Stack Trace:</strong><br />{2}</p>", ex.GetType().ToString(), ex.Message, ex.StackTrace.Replace(System.Environment.NewLine, "<br />")) 
     End If 
    End If 

End Sub 

本地它工作的很好。當我部署到運行IIS 7的Windows Server 2008計算機時,ex的條件不會每次都被觸發。 我猜這個問題與IIS版本有關 - 只是因爲我看到了一些其他帖子,但沒有解決方案。如果有人對我如何完成錯誤消息轉儲有想法,我會很感激。我想知道爲什麼這也發生。我的意思是,一個解決方案是偉大的,但我很好奇爲什麼。

回答

0

有在 Vista中的一個非常相似的已知錯誤,這是所謂固定在 SP1,但它看起來像相同的修訂 是不能在Windows 2008 Server的一部分呢。 有一種變通方法,但 - 如果你 設置網站的默認錯誤財產 (下IIS設置 - >錯誤頁面 - > 編輯功能設置...),以自定義 頁面(見下文),IIS將調用此 頁每當錯誤不被明確配置 狀態碼處理(所以你的404,等 處理程序仍然可以工作)處理 - 但 某些原因,處理錯誤這 方式意味着服務器.GetLastError()仍然 正常工作。

查看原文博文:Fun with Server.GetLastError() in classic ASP on Windows Server 2008

我想它與您的情況類似,也可能對您有所幫助。

0

另一種方法是在web.config中使用 ASP.NET 3.5 SP1中引入的新參數redirectMode

redirectMode="ResponseRewrite" 模式允許加載錯誤頁面 不重定向瀏覽器,網址 保持不變,而異常將 不會丟失。

這是answeredASP.NET自定義錯誤頁 - Server.GetLastError()爲null問題。查看更多細節。