我被赋予了重写我们的异常处理系统的激动人心的任务。虽然我会声明从应用程序范围的角度处理异常不是我们想要的,但通常情况下,当我们的团队人手不足时,我们需要推出大量工作,这是不可避免的,所以请不要燃烧此处异常处理的全球化解决方案:)
我已经很好地寻找存在哪些常见的解决方案。目前,我们使用 Global.asax 和 Application_Error 事件来执行 Server.GetLastError() ,它被置于会话状态,然后调用重定向到另一个页面,然后检索会话数据并以人类可读的格式输出。重定向还调用一个 sproc,它会仔细审核错误信息,这些信息是 a) 通过电子邮件发送给开发人员和 b) 从只有开发人员可以查看的网页查看。
我见过的做事的新方法是使用 IHttpModule 接口,使用 App_Code 中的一个类来做一些事情(这是我的快速实现)
Imports Microsoft.VisualBasic
Public Class ErrorModule : Implements IHttpModule
Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
' Not used
End Sub
Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
AddHandler context.Error, AddressOf context_Error
End Sub
Public Sub context_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim ex As Exception = HttpContext.Current.Server.GetLastError
' do something with the error
' call the stored procedure
' redirect the user to the error page
HttpContext.Current.Server.ClearError()
HttpContext.Current.Response.Redirect("index.htm")
End Sub
End Class
我的问题是,与使用 Global.asax 事件相比,此解决方案有什么好处?此外,将数据交给错误页面的最佳方式是什么?
编辑:顺便说一句,上面的代码确实有效;)
编辑:另外,HttpModule 是如何在幕后工作的?它只是在应用程序启动时将错误事件注册到该特定功能吗?
更新:
经过进一步调查,在使用 IHttpModule 接口时,获取会话数据似乎非常非常混乱。我认为 MS 的 HttpModule 还不够成熟,无法在我们的特定场景中使用——除非有特定于会话数据的事件,否则我们无法使用它。