Dot-Net
如何在 ASP.NET MVC 中記錄未處理的異常?
這是我在 Global.asax.vb 中嘗試做的事情:
Public Class MvcApplication Inherits System.Web.HttpApplication Shared Sub RegisterRoutes(ByVal routes As RouteCollection) routes.IgnoreRoute("{resource}.axd/{*pathInfo}") routes.MapRoute( _ "Error", _ "error.html", _ New With {.controller = "Error", _ .action = "FriendlyError"} _ ) ... 'other routes go here' ... End Sub Sub Application_Start() RegisterRoutes(RouteTable.Routes) End Sub Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) ... 'code here logs the unhandled exception and sends an email alert' ... Server.Transfer("http://www.example.com/error.html") End Sub End Class但是那個 Server.Transfer 失敗了:
Invalid path for child request 'http://www.example.com/error.html'. A virtual path is expected.我該如何解決?或者,我有什麼更好的方法來做到這一點?
我剛剛在 Scott Hanselman 的名為 ELMAH 的部落格上找到了這個,它是一個錯誤記錄器/處理程序,您可以使用它而無需更改程式碼。您可能想研究一下,因為它似乎與 MVC 很好地配合使用。
ELMAH是一個出色的錯誤處理程序,用於擷取未處理的異常。它通過 HttpModules 無縫插入您的 Web 應用程序,並具有各種通知和日誌記錄選項。
特徵:
- SQL、XML、SQLite、InMemory 日誌記錄
- 電子郵件通知
- RSS訂閱
- 詳細的!記錄異常
- 易於集成
- 錯誤信號 - 在為使用者“死得好”的同時向錯誤處理程序發出信號