Page 1 of 1
Global.asax and application space
4 Replies - 4689 Views - Last Post: 30 October 2007 - 03:04 AM
Posted 17 October 2007 - 05:22 AM
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!
Replies To: Global.asax
Posted 17 October 2007 - 05:32 AM
Posted 17 October 2007 - 08:01 AM
thanks for the suggestion, ill go and check that out.
Posted 19 October 2007 - 10:54 AM
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
Posted 30 October 2007 - 03:04 AM
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