Error Pages, Static Error Variables and all the stuff inbetween

G

Girish

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
 
S

Steven Cheng[MSFT]

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
 
G

Girish

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top