RE: ASP.NET Exception Handling
- From: Peter Bromberg [C# MVP] <pbromberg@xxxxxxxxxxxxxxxxxxx>
- Date: Sat, 22 Apr 2006 18:19:01 -0700
Put this code in the Application_Error event handler in your global.asax
Something like so:
// in global.asax:
protected void Application_Error(Object sender, EventArgs e)
{
// logging code line follows
ExceptionHandler.LogException exc = new ExceptionHandler.LogException();
Exception ex = Server.GetLastError().GetBaseException();
// pass ex to your logging or notification utility, write to event log, or
whatever.
}
And here is something you might find even more useful:
http://www.eggheadcafe.com/articles/20030816.asp
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
"Brad Baker" wrote:
I'm trying to create a custom ASP.NET custom error page however I am having.
some problems. Here is what I have done:
1) I've created a page called default.aspx with the following code to
stimulate an error:
<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e) {
throw new Exception ("Test Exception");
}
</script>
2) I've edited my web.config file custom errors as follows:
<customErrors
mode="On"
defaultRedirect="errorreport.aspx"
/>
3) My errorreport.aspx contains the following code:
<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e) {
HttpContext ctx = HttpContext.Current;
Exception ex = Server.GetLastError().GetBaseException();
string errorInfo =
"<br>Offending URL: " + ctx.Request.Url.ToString () +
"<br>Source: " + ex.Source +
"<br>Message: " + ex.Message +
"<br>Stack trace: " + ex.StackTrace;
Response.Write (errorInfo);
}
</script>
When I access default.aspx, I get redirected to asperrorreport.aspx as
expected but the following error is generated on the errorreport.aspx page:
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.
Line 12: Exception ex = Server.GetLastError().GetBaseException();
I believe this is because the "ex" variable is null. What I don't understand
is why it's null? What am I missing?
Thanks,
Brad
- Follow-Ups:
- RE: ASP.NET Exception Handling
- From: Steven Cheng[MSFT]
- RE: ASP.NET Exception Handling
- References:
- ASP.NET Exception Handling
- From: Brad Baker
- ASP.NET Exception Handling
- Prev by Date: Re: asp:Button click at server
- Next by Date: Re: PP:Displaying Default Images when required does not exist
- Previous by thread: ASP.NET Exception Handling
- Next by thread: RE: ASP.NET Exception Handling
- Index(es):
Relevant Pages
|