Global.asax

Global.asax and application space

Page 1 of 1

4 Replies - 4545 Views - Last Post: 30 October 2007 - 03:04 AM Rate Topic: -----

#1 jp42  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-August 07

Global.asax

Posted 17 October 2007 - 05:22 AM

Hi all,


I was hoping to get a little insight into the global.asax file. After reading a bit about the global.asax file I see that certain events are raised based on the first instance of an HttpApplication object being created or based on the creation of any given HttpApplication object. In the global.asax I wanted to create a thread which for any given HttpApplication object instance, if an exception is caught a thread will write some error info to a table in a database. So, i want to create a thread in the global.asax file and then have it sleep, only to wake up when an exception is caught and write the db error record. I was a bit unsure of which event in the global.asax file would i create and initialize the error thread? I hope I have explained myself well enough...any thoughts/opinions regarding the global.asax or threading beyond the scope of this question are also welcome ...I am also trying to understand asp.net at a higher level with regards to application space and go beyond just the page level processing...thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Global.asax

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1637
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Global.asax

Posted 17 October 2007 - 05:32 AM

Well for starters you don't need a Thread for this, you can do all this in the Application_Error Event. This will catch any unhanded exceptions as well as exceptions you've caught with a Try...Catch.
Was This Post Helpful? 0
  • +
  • -

#3 jp42  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-August 07

Re: Global.asax

Posted 17 October 2007 - 08:01 AM

View PostPsychoCoder, on 17 Oct, 2007 - 05:32 AM, said:

Well for starters you don't need a Thread for this, you can do all this in the Application_Error Event. This will catch any unhanded exceptions as well as exceptions you've caught with a Try...Catch.


psycho,

thanks for the suggestion, ill go and check that out.
Was This Post Helpful? 0
  • +
  • -

#4 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Global.asax

Posted 19 October 2007 - 10:54 AM

Yeah, he's right. I just finished taking a class on ASP.Net, and this is something that we learned how to do. Basically, this is what you'd put inside your global.asax file:
Sub Application_Error(ByVal sender As Object, ByVal e As System.EventArgs)
	Server.Transfer("appError.aspx")
End Sub


Which would then transfer your server data to appError.aspx in the case of a problem. Inside appError.aspx, we had:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	Dim appError As System.Exception = Server.GetLastError()
	Label1.Text = "Error Message: " & AppError.Message
	Label1.Text &= "<p>Base Error Message: " & appError.GetBaseException.Message
	If(TypeOf(appError) Is HttpException) Then
		Dim checkException As HttpException = CType(appError,HttpException)
		Select Case checkException.GetHttpCode
			Case 403
				Label2.Text = "You are not allowed to view that page."
			Case 404
				Label2.Text = "The page you requested cannot be found."
			Case Else
				Label2.Text = "The server has experienced an error."
		End Select
	Else
		' The exception was not an HttpException
		Label2.Text = "The following error occured:<br />" & appError.ToString()
	End If
	Label2.Text &= "<p>Please contact the server administrator.</p>"
	Server.ClearError()
End Sub


appError.aspx also had two labels inside of it, named Label1 and Label2 so that we could output our error messages.

To call the custom error handler we built, we created a really quick sample page with a button and this code on it's clicked event:
Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
	Throw New Exception("This is my error text")
End Sub



That worked for us; it's not exactly doing anything complex, but I think that all you'd need to change is what appError.aspx did with it's error information.

This post has been edited by girasquid: 19 October 2007 - 10:55 AM

Was This Post Helpful? 0
  • +
  • -

#5 mattw  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 02-September 07

Re: Global.asax

Posted 30 October 2007 - 03:04 AM

Hey jp42,

It may be worth you checking out log4net it's a pretty decent library to standardize on for logging. It has a number of standard appenders for handling writing logs to files / emails / databases etc. Check it out, it may save you some hassle :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1