Application_Error:如何保存异常信息?
我使用以下代码来捕获应用程序中的异常,并将它们保存在 Session 对象中。然后,从应用程序自动重定向到的错误处理程序页面中的 Session 检索异常:
protected virtual void Application_Error(Object sender, EventArgs e)
{
try
{
Exception ex = Server.GetLastError();
Session["exception"] = ex;
}
catch { }
}
此代码有一个问题:
如果格式错误的路径进入,则 Session
不可用:"example .com/"foo"
- 访问它时抛出异常,并在错误页面中从 Session 对象中检索到 NULL
在应用程序中保存异常信息并将其传递给错误的更好方法是什么处理程序操作?
I am using the following code to capture exceptions in the application, and save them in the Session object. The exception is then retrieved from Session in the error handler page to which the app automatically redirects:
protected virtual void Application_Error(Object sender, EventArgs e)
{
try
{
Exception ex = Server.GetLastError();
Session["exception"] = ex;
}
catch { }
}
I have one problem with this code:
Session
is not available if a malformed path gets in: "example.com/"foo"
- Exception is thrown when accessing it, and NULL is retrieved from Session object in the error page
What is a better way to save exception information in the application and pass it to the error handler action?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您尝试记录异常,请查看 elmah 项目。
Scott Hanselman 有很好的介绍
If you are trying to log exceptions then take a look at the elmah project.
Scott Hanselman has a good introduction
如果您因任何原因无法使用 ELMAH,
请http://www.elmah.org genericerror.com/blog/2009/01/27/ASPNetMVCCustomErrorPages.aspx
If you can't use ELMAH for any reason,
http://www.genericerror.com/blog/2009/01/27/ASPNetMVCCustomErrorPages.aspx