Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to know in which page of the web app the exception occurred? 1

Status
Not open for further replies.

ericnet

Programmer
Mar 29, 2006
106
In order to get in which page an exception occurred, which is the best method I can use?

Request.QueryString.ToString()
Request.Url.AbsoluteUri
Request.Path
Request.Url.ToString()
... Others?


I need this information in Sub Application_Error() of Global.asax and in a Try/Catch block in the page, when logging the exception in Windows log file and reporting via mail:

(global.asax file)
Code:
<script language="VB" runat="server">

 Sub Application_Error(Sender As Object, E As EventArgs)

  Dim ex As Exception = Server.GetLastError().GetBaseException()

  EventLog.WriteEntry("Test Web", "Message: " & ex.Message & _
     "\nSOURCE: " & ex.Source & "\nTARGETSITE: " & ex.TargetSite & _
     "\nSTACKTRACE: " & ex.StackTrace & [b]“\nPAGE/URL” & _ Request.Url.ToString()[/b], _ etc,..

  ‘Here send e-mail notification to administrator with same information logged above
   ...
   message.Body = "<html><body><h1>[b]Page: " & Request.Url.ToString()[/b] & "</h1>" ............ "</body></html>"
	
    SmtpMail.Send(MyMessage)

 End Sub

</script>



(In the page)
Code:
Sub Page_Load(Sender As Object, E As EventArgs)
 Try

  ‘Code here 

 Catch ex As Exception 

  EventLog.WriteEntry("Test Web", "Message: " & ex.Message & _
      "\nSOURCE: " & ex.Source & "\nTARGETSITE: " & ex.TargetSite & _
      "\nSTACKTRACE: " & ex.StackTrace & [b]“\nPAGE/URL” & _ Request.Url.ToString()[/b], _ etc,..


   ‘Here send e-mail notification to administrator with same information logged above
   ...
   message.Body = "<html><body><h1>[b]Page: " & Request.Url.ToString()[/b] & "</h1>" ............ "</body></html>"
	
    SmtpMail.Send(MyMessage)  

 End Try
End Sub

Thank you
 
try using the HttpContext.Current object...

Known is handfull, Unknown is worldfull
 
HttpContext.Current to get the page where the exception occurred......???
 
Yes, e.g.
Code:
HttpContext.Current.Request.Url.ToString


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi,

And which is the utility of using HttpContext.Current before any Request? I mean for example, what' s the difference between use 'HttpContext.Current.Request.Url.ToString' and the normal 'Request.Url.ToString'?
 
Nothing really as it's the same object but from files such as the global.asax file, you'll have to reference HttpContext.Current otherwise you won't be able to get a reference to the Request object.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Recently I tried to send an e-mail from global.asax from Sub Application_Error() with this info 'Request.UserHostAddress' (without use HttpContext.Current), and I succesfully received the IP address. Is this because I run my web app locally (in server machine)? When the app will run from other machine than server, I will need to use 'HttpContext.Current.Request.UserHostAddress'?
 
I have read msdn documentation about HttpContext and HttpRequest.ServerVariables, but it is hardly explained…

Which is the purpose of use HttpRequest.ServerVariables property? Because I have seen developers that for example uses Request.ServerVariables("HTTP_USER_AGENT") instead of simply use Request.UserAgent. Is it for the same purpose than HttpContext.Current is used?

I don’ t really have clear the purpose of HttpContext and HttpRequest.ServerVariables property. Are both classes designed to access object data temporary stored in the server memory when we are not in the requested page that had the object data? As you said?:

Nothing really as it's the same object but from files such as the global.asax file, you'll have to reference HttpContext.Current otherwise you won't be able to get a reference to the Request object.
 
The HttpRequest.ServerVariables class is quite different to the HttpContect class. Add Trace="True" to your Page directive in the aspx file, and you'll see a list of the different server variables that you can access.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Ok I see.. Then I only want to know which method is better to get this information (in bold) from global.asax and page level when an exception is thrown:

User IP address
User-agent
Requested page
Cookies sent
Host name of the client

According to what I know now, there are exactly 4 ways to get the same data for each information I need:

User IP address:
Request.UserHostAddress
Request.ServerVariables("REMOTE_ADDR")
HttpContext.Current.Request.UserHostAddress
HttpContext.Current.Request.ServerVariables("REMOTE_ADDR")

User-agent:
Request.UserAgent
Request.ServerVariables("HTTP_USER_AGENT")
HttpContext.Current.Request.UserAgent
HttpContext.Current.Request.ServerVariables("HTTP_USER_AGENT")

Requested page:
Request.Url.ToString()
Request.ServerVariables("URL")
HttpContext.Current.Url.ToString()
HttpContext.Current.Request.ServerVariables("URL")

Cookies sent:
Request.Cookies()
Request.ServerVariables("HTTP_COOKIE")
HttpContext.Current.Request.Cookies()
HttpContext.Current.Request.ServerVariables("HTTP_COOKIE")

Host name of the client:
Request.UserHostName
Request.ServerVariables("REMOTE_HOST")
HttpContext.Current.Request.UserHostName
HttpContext.Current.Request.ServerVariables("REMOTE_HOST")


My question is:
Which method is better to get every info from global.asax, and which method is better to get them from page level?

Thanks
 
anthing will do, there is no difference between these methods as ALL objects point to the same.

e.g.:
both
Request.UserHostName
HttpContext.Current.Request.UserHostName

point to the same object. at page level the Request object can be accessed directly (the best way), but at other places (like HttpModules etc) the HttpContext is the best object...



Known is handfull, Unknown is worldfull
 
Cool :), vbkris thank you very much for your advice!

 
Nothing really as it's the same object but from files such as the global.asax file, you'll have to reference HttpContext.Current otherwise you won't be able to get a reference to the Request object.
Which is what I pointed out in the above post.





____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top