Handling the Events
Let's take a look at a sample Global.asax
file -
<%@ Import Namespace="System.Diagnostics"
%>
<script runat="server">
Sub
Application_Start(ByVal
sender As
Object,
ByVal e
As EventArgs)
EventLog.WriteEntry("Sample Application", "Application Started!",
EventLogEntryType.Information)
End
Sub
Sub Session_Start(ByVal
sender As
Object,
ByVal e
As EventArgs)
Session.Contents.Add("TimeStart", DateTime.Now())
End
Sub
Sub Application_Error(ByVal
sender As
Object,
ByVal e
As EventArgs)
EventLog.WriteEntry("Sample Application", "Application Error Occured!",
EventLogEntryType.Error)
End
Sub
Sub Application_End(ByVal
sender As
Object,
ByVal e
As EventArgs)
EventLog.WriteEntry("Sample Application", "Application Ended!",
EventLogEntryType.Warning)
End
Sub
</script> |
As you can see, this handles four events.
When the application starts or ends, it
writes a semi-informative message to the system Event Log.
On an error, it writes an almost-useless message to the EventLog.
When a session begins (basically whenever someone visits the site) it adds a
new session variable with the time they logged in (possibly a waste of session
space).
Notice how it looks almost like a normal
ASP.NET page, but without the HTML content.
ASP.NET will automatically compile and
process this file.