Application_Error: how to save exception information?
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 virtu开发者_运维技巧al 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?
If you are trying to log exceptions then take a look at the elmah project.
Scott Hanselman has a good introduction
If you can't use ELMAH for any reason,
http://www.genericerror.com/blog/2009/01/27/ASPNetMVCCustomErrorPages.aspx
精彩评论