customErrors

Discussion in 'ASP .Net' started by Arne, Aug 11, 2009.

  1. Arne

    Arne Guest

    Can I retrieve the exception, querystring, form variables after a redirect to
    an
    error page specified in the web.config?
    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.
    Server.GetLastError().GetBaseException() does not work in error.aspx.

    <customErrors mode="On" defaultRedirect="Error.aspx">
    <error statusCode="404" redirect="FileNotFound.htm" />
    </customErrors>
    --
    Certified Geek, Professional Data Dude
    (Doing classic ASP to get paid, and WPF for fun.)
    Arne, Aug 11, 2009
    #1
    1. Advertising

  2. =?Utf-8?B?QXJuZQ==?= <> wrote in
    news::

    > 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! |
    *******************************************
    Gregory A. Beamer, Aug 12, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. NRao

    customErrors 404

    NRao, Jul 1, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    464
    Bassel Tabbara [MSFT]
    Jul 1, 2003
  2. Jesse Napier

    CustomErrors statusCode 500

    Jesse Napier, Aug 7, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    4,758
    Michal A. Valasek
    Aug 7, 2003
  3. =?Utf-8?B?cmV6YQ==?=

    <customErrors .... not working??

    =?Utf-8?B?cmV6YQ==?=, Feb 18, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    982
    Jim Cheshire [MSFT]
    Feb 18, 2004
  4. Pete Davis

    customErrors issues

    Pete Davis, Mar 2, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    381
    Pete Davis
    Mar 6, 2004
  5. =?Utf-8?B?U2FtdWVs?=

    .NET question about the customErrors Tag

    =?Utf-8?B?U2FtdWVs?=, May 2, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    453
    Steven Cheng[MSFT]
    May 11, 2004
Loading...

Share This Page