Welcome to Dream.In.Code
Become an Expert!

Join 149,586 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,776 people online right now. Registration is fast and FREE... Join Now!




Global.asax

 
Reply to this topicStart new topic

Global.asax, Global.asax and application space

jp42
17 Oct, 2007 - 04:22 AM
Post #1

New D.I.C Head
*

Joined: 21 Aug, 2007
Posts: 38


My Contributions
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!
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Global.asax
17 Oct, 2007 - 04:32 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,482



Thanked: 161 times
Dream Kudos: 9050
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

jp42
RE: Global.asax
17 Oct, 2007 - 07:01 AM
Post #3

New D.I.C Head
*

Joined: 21 Aug, 2007
Posts: 38


My Contributions
QUOTE(PsychoCoder @ 17 Oct, 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.


psycho,

thanks for the suggestion, ill go and check that out.

User is offlineProfile CardPM
+Quote Post

girasquid
RE: Global.asax
19 Oct, 2007 - 09:54 AM
Post #4

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,294



Thanked: 18 times
Dream Kudos: 725
My Contributions
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:
CODE

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:
CODE

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:
CODE

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 Oct, 2007 - 09:55 AM
User is offlineProfile CardPM
+Quote Post

mattw
RE: Global.asax
30 Oct, 2007 - 02:04 AM
Post #5

New D.I.C Head
*

Joined: 2 Sep, 2007
Posts: 23


My Contributions
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 smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:02PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month