Re: customErrors

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



=?Utf-8?B?QXJuZQ==?= <Arne@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
news:F41D899E-3E81-4C74-9C7F-6D18A9C7F0F0@xxxxxxxxxxxxx:

Can I retrieve the exception, querystring, form variables after a
redirect to an
error page specified in the web.config?

Not directly.

I need to create error logging.
I know about Application_Error handler in global.asax, but it does
not seem to be suited for me.

Why not, the need to get all of the form elements? If so, see below.

Server.GetLastError().GetBaseException() does not work in error.aspx.

No, it will not, as the error has already been consumed in the event,
even if you have no application error event handler.

<customErrors mode="On" defaultRedirect="Error.aspx">
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>

There is nothing easy about this. You have two choices:

1. Figure out where the potential error-ridden code is and pull values
prior to this point.

2. use the Application_Error to pull all form elements (and perhaps
other Request elements

Sample code:

void Application_Error(object sender, EventArgs e)
{
// Get page that threw the error
HttpContext context = HttpContext.Current;
string url = context.Request.Url.ToString();

Exception exception = Server.GetLastError();
//Log ex

if (exception.InnerException != null)
{
Exception inner = exception.InnerException;
//Log inner exception
}

Exception baseException = exception.GetBaseException();
//Log base

//Get context to get page elements
HttpRequest request = context.Request;

//Pulls form elements
foreach (string key in Request.Form.AllKeys)
{
string value = Request.Form[key];
//Log the value here
}
}


This is fine for first time encounters with errors, but a persistent
problem using this route is very sloppy.

Unwinding the exception object can be done like so:

private void LogException(Exception ex)
{
string type = ex.GetType().ToString();
string message = ex.Message;
string stackTrace = ex.StackTrace;
string targetSite = ex.TargetSite;

//May also want data (rarely, but possible)
//Log elements here
}

And, if you want to handle the inner exception without explicitly coding
a separate section, you can unwind a whole stack of exceptions like so:

private void LogException(Exception ex, int indent)
{
string type = ex.GetType().ToString();
string message = ex.Message;
string stackTrace = ex.StackTrace;
string targetSite = ex.TargetSite;

//May also want data (rarely, but possible)
//Log using indent so you cascade the exceptions
// to more easily figure exception nesting

if (ex.InnerException != null)
{
LogException(ex, indent + 4);
}
}

The initial call is then:

LogException(ex, 0);

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
.



Relevant Pages

  • Re: xmalloc string functions
    ... char *dup; ... tokis strtokthat takes a string argument instead of using a global. ... better though, would be an exception handling system, namely, we crash only if the out-of-memory exception goes unhandled. ... likely an unwinding handler would silently ignore any previous recursive handlers. ...
    (comp.lang.c)
  • Re: Structured exception information
    ... So that the handler has more information about the error? ... result and I want to raise an exception. ... All of this can be put into the string by the routine raising the ... If you want a GUI pop-up, and it would be wrong to have the routine ...
    (comp.lang.ada)
  • RE: Detailed ASP.Net info not displaying in browser
    ... HTTP 500 page when an exception is thrown from the web service. ... at VOSE.Data.DataRequest.dr_DB2Process.DeleteCommRows(String company, String ... objQueue, tOrderManagementIndicator omindicator, Int32 intTriggerId) in ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.xml)
  • RE: Detailed ASP.Net info not displaying in browser
    ... HTTP 500 page when an exception is thrown from the web service. ... at VOSE.Data.DataRequest.dr_DB2Process.DeleteCommRows(String company, String ... objQueue, tOrderManagementIndicator omindicator, Int32 intTriggerId) in ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.xml)
  • Re: App_data - ASPNETDB.MDF
    ... is only because I want my site work, without exception. ... Server Error in '/' Application. ... serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 ...
    (microsoft.public.dotnet.framework.aspnet)