Error Pages, Static Error Variables and all the stuff inbetween

Discussion in 'ASP .Net' started by Girish, Feb 20, 2004.

  1. Girish

    Girish Guest

    so I was looking at an example on codeproject.com that talked about how to
    globalize your error handling. I was extremely confused by some code. Id
    appreciate if someone could help me out by understanding this.

    URL if anyone is interested:
    http://www.codeproject.com/aspnet/JcGlobalErrorHandling.asp#xx726891xx


    1) Ok, so in short, the way to do this error handling is to set a STATIC
    variable in some class whenever an error occurs. Global.aspx file:

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    Utilities.LastError = Server.GetLastError()
    End Sub

    2) Decl of Utility Class:
    Public Class Utilities
    Public Shared LastError As System.Exception
    End Class

    3) Obviously set the customErrors handler in web.config:
    <customErrors mode="On" defaultRedirect="ErrorPage.aspx">

    4) In the ErrorPage.aspx page, you have the following code to get the error
    back from the STATIC variable:
    With Utilities.LastError.InnerException
    Dim r0 As DataRow = t.NewRow
    r0(0) = "Message"
    r0(1) = .Message
    t.Rows.Add(r0)
    Dim r1 As DataRow = t.NewRow
    r1(0) = "StackTrace"
    r1(1) = .StackTrace.Replace(vbCr, "<br>")
    t.Rows.Add(r1)
    Dim r2 As DataRow = t.NewRow
    r2(0) = "Source"
    r2(1) = .Source
    t.Rows.Add(r2)
    End With



    Sorry for the mixing up of concepts/code between c# and vb.net, but heres my
    problem: I understand what static variables are. And I understand that once
    set, they are set for the whole application process. (Im stating process -
    but maybe its threads. I dont really know. Does IIS choose from pool or
    create one thread per one hit to the application webiste?)

    Anyways, so if someone(User1) were to come on the website and an error
    occurred, the static variable would be set and the person would be
    redirected. Now what in the name of the world would happen IFF before the
    redirect to the errorpage, User1's processing gets preempted and ANOTHER
    user (User2) were to hit the website and get a different exception. The SAME
    static variable would be set to a new error. Right?

    Now processing continues and User1's error page renders with User2's error
    code???!

    I dont get it.

    Girish
     
    Girish, Feb 20, 2004
    #1
    1. Advertising

  2. Hi Girish,


    Thanks for posting in the community!
    I've read the tech article you provided and your question is on the means
    the author used to store the server error in Application_Error event when
    unhandled error occurs on server , yes?

    The author used a static class member to store the error exception. Of
    course, I don't quite agree to this solution since the static member of a
    class is accessable to the whole application as you mentioned and will
    cause concurrence issue.

    The reason why we need to pre-store the error info in Application_Error is
    because by default the ASP.NET will clear the server error and restart a
    new session after the Application_Error event and before the user
    redirected to the "defaultErrorPage" you set in the <customError> element
    in web.config file. You can try using the Server.GetLastError in
    the errorpage's Page_load event to see whether you can get the info, you
    'll get a null value. However, we can workaround this by manually use
    Server.Transfer to redirect user to the error page in Application_Error
    event, for example:
    protected void Application_Error(Object sender, EventArgs e)
    {
    Server.Transfer("customerrorpage.aspx");
    }
    Thus, we can still use Server.GetLastError to get the error info. No
    additional store needed, of course no concurrece problem. Also, I've
    discussed this issue in another post ,here is its web link in google:

    #DefaultRedirect Problem
    http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&frame=right&th=c
    4385267d67065bd&seekm=%24EyuIys6DHA.568%40cpmsftngxa07.phx.gbl#link1

    You may also have a look to see my detailed description.

    Furthur more, I think the "Log4Net" logging component the author has
    mentioned , I think it is a very cool component.
    #log4net
    http://log4net.sourceforge.net/

    Please check out the above items. If you feel anything unclear, please feel
    free to post here.


    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
     
    Steven Cheng[MSFT], Feb 20, 2004
    #2
    1. Advertising

  3. Hi Girish,

    Have you had a chance to check out my suggestions or have you got any
    further ideas on this issue? If you need any further help, please feel free
    to post here.


    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
     
    Steven Cheng[MSFT], Feb 24, 2004
    #3
  4. Girish

    Girish Guest

    Yes, thanks Steven. Im not using global static variables. Ive used the
    server.transfer method call to facilitate my error handling.

    Thanks for your help.
    Girish

    "Steven Cheng[MSFT]" <> wrote in message
    news:i24reQu%...
    > Hi Girish,
    >
    > Have you had a chance to check out my suggestions or have you got any
    > further ideas on this issue? If you need any further help, please feel

    free
    > to post here.
    >
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    > Get Preview at ASP.NET whidbey
    > http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    >
     
    Girish, Feb 24, 2004
    #4
    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. Mike Miller
    Replies:
    1
    Views:
    392
    Murat G.
    Oct 19, 2003
  2. SaravanaKumar
    Replies:
    6
    Views:
    9,408
    Tony Morris
    Oct 19, 2004
  3. gamehack
    Replies:
    3
    Views:
    585
    gamehack
    Dec 23, 2005
  4. jason

    difference inbetween macro & function

    jason, Nov 6, 2007, in forum: C Programming
    Replies:
    6
    Views:
    433
    Charlie Gordon
    Nov 8, 2007
  5. Replies:
    54
    Views:
    481
    Michele Dondi
    Jan 16, 2005
Loading...

Share This Page