Home
Contact Us
Log In
Register
Vital Web Tools
Privacy
Purchase
About Us
Sample Global.asax
<%@ Application Language="VB" %> <script runat="server"> Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs on application startup Dim Share As ShareClass Dim Logs As LogsClass Dim Log As LogClass Dim AspNetCookieNames() As String 'Dim Configuration As System.Configuration.Configuration Me.Application.Lock() If (Me.Application("Shared") Is Nothing) Then Logs = New LogsClass Log = New LogClass("Data-") 'Required Name Log.Path = Hosting.HostingEnvironment.ApplicationPhysicalPath & "Logs\" Log.IncludeTime = True Log.LineWidth = 110I Logs.Add(Log) Log = New LogClass("Trace-") Log.Path = Hosting.HostingEnvironment.ApplicationPhysicalPath & "Logs\" Log.IncludeTime = True Log.LineWidth = 110I Logs.Add(Log) Log = New LogClass("Errors-") 'Required Name Log.Path = Hosting.HostingEnvironment.ApplicationPhysicalPath & "Logs\" Log.IncludeTime = True Log.LineWidth = 110I Logs.Add(Log) Log = New LogClass("Unhandled-") Log.Path = Hosting.HostingEnvironment.ApplicationPhysicalPath & "Logs\" Log.IncludeTime = True Log.LineWidth = 110I Logs.Add(Log) Log = New LogClass("Activity-") Log.Path = Hosting.HostingEnvironment.ApplicationPhysicalPath & "Logs\" Log.IncludeTime = True Log.LineWidth = 110I Logs.Add(Log) Share = New ShareClass(Date.UtcNow, Me.Server, Logs) ReDim AspNetCookieNames(1I) 'Based on configurations and Net Framework classes you are using set the names for all cookies created/used 'by ASP.NET - This prevents code from accidentally removing cookies used by the ASP.NET Framework Try '***** Something is wrong - made several variations to .OpenWebConfiguration '***** And Always get a Security Exception for the Machine.config Which I Am NOT REQUESTING 'Configuration = Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Nothing) 'AspNetCookieNames(0I) = DirectCast(Configuration.GetSection("system.web/sessionState"), _ ' Web.Configuration.SessionStateSection).CookieName '"ASP.NET_SessionId" is default Catch ex As Exception Logs("Errors-").WriteException(ex) End Try AspNetCookieNames(0I) = "ASP.NET_SessionId" 'is default AspNetCookieNames(1I) = FormsAuthentication.FormsCookieName 'usually .ASPXFORMSAUTH Share.AspNetCookieNames = AspNetCookieNames Share.Sessions.SetCreateSessionDelegate = AddressOf Me.CreateSession Me.Application.Add("Shared", Share) Else Share = DirectCast(Me.Application("Shared"), ShareClass) End If Share.Logs("Activity-").WriteLine("Application_Start") Share.Logs("Activity-").Flush() Me.Application.UnLock() GC.SuppressFinalize(Me) End Sub Sub Application_End(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs on application shutdown 'A simple trace log has been placed in here because Microsoft wisdom was to finalize all objects before 'calling Application_End (Instead of after which makes more sense.) 'I have added GC.SuppressFinalize where needed but I still don't completely trust Microsofts design. Dim Disposable As IDisposable Dim ReasonText As String Dim EndTime As Date Dim Share As ShareClass = Nothing Dim Trace As IO.StreamWriter = Nothing Dim TraceName As String TraceName = "Logs\ApplicationEnd-" & Date.UtcNow.Date.ToString("yyyy-MM-dd") & ".log" Try Try Share = DirectCast(Me.Application("Shared"), ShareClass) Catch ex As Exception Trace = IO.File.AppendText(Hosting.HostingEnvironment.ApplicationPhysicalPath & TraceName) Trace.AutoFlush = True Trace.WriteLine(Date.UtcNow.ToString("hh:mm:ss.ff tt ")) Trace.WriteLine(ex.Source) Trace.WriteLine(ex.Message) If (ex.StackTrace IsNot Nothing) Then Trace.WriteLine(ex.StackTrace) ex = ex.InnerException Do While (ex IsNot Nothing) Trace.WriteLine(ex.Source) Trace.WriteLine(ex.Message) If (ex.StackTrace IsNot Nothing) Then Trace.WriteLine(ex.StackTrace) ex = ex.InnerException Loop End Try Select Case Hosting.HostingEnvironment.ShutdownReason Case ApplicationShutdownReason.BinDirChangeOrDirectoryRename ReasonText = "BinDirChangeOrDirectoryRename" Case ApplicationShutdownReason.BrowsersDirChangeOrDirectoryRename ReasonText = "BrowsersDirChangeOrDirectoryRename" Case ApplicationShutdownReason.BuildManagerChange ReasonText = "BuildManagerChange" Case ApplicationShutdownReason.ChangeInGlobalAsax ReasonText = "ChangeInGlobalAsax" Case ApplicationShutdownReason.ChangeInSecurityPolicyFile ReasonText = "ChangeInSecurityPolicyFile" Case ApplicationShutdownReason.CodeDirChangeOrDirectoryRename ReasonText = "CodeDirChangeOrDirectoryRename" Case ApplicationShutdownReason.ConfigurationChange ReasonText = "ConfigurationChange" Case ApplicationShutdownReason.HostingEnvironment ReasonText = "HostingEnvironment" Case ApplicationShutdownReason.HttpRuntimeClose ReasonText = "HttpRuntimeClose" Case ApplicationShutdownReason.IdleTimeout ReasonText = "IdleTimeout" Case ApplicationShutdownReason.InitializationError ReasonText = "InitializationError" Dim le As Exception = Me.Server.GetLastError Do While (le IsNot Nothing) If (Share IsNot Nothing) Then Share.Logs("Unhandled-").WriteException(le) Share.Logs("Unhandled-").Flush() Else If (Trace Is Nothing) Then Trace = IO.File.AppendText(Hosting.HostingEnvironment.ApplicationPhysicalPath & TraceName) Trace.AutoFlush = True End If Trace.WriteLine(Date.UtcNow.ToString("hh:mm:ss.ff tt ")) Trace.WriteLine(le.Source) Trace.WriteLine(le.Message) If (le.StackTrace IsNot Nothing) Then Trace.WriteLine(le.StackTrace) le = le.InnerException Do While (le IsNot Nothing) Trace.WriteLine(le.Source) Trace.WriteLine(le.Message) If (le.StackTrace IsNot Nothing) Then Trace.WriteLine(le.StackTrace) le = le.InnerException Loop End If Me.Server.ClearError() le = Me.Server.GetLastError Loop Case ApplicationShutdownReason.MaxRecompilationsReached ReasonText = "MaxRecompilationsReached" Case ApplicationShutdownReason.None ReasonText = "None" Case ApplicationShutdownReason.PhysicalApplicationPathChanged ReasonText = "PhysicalApplicationPathChanged" Case ApplicationShutdownReason.ResourcesDirChangeOrDirectoryRename ReasonText = "ResourcesDirChangeOrDirectoryRename" Case ApplicationShutdownReason.UnloadAppDomainCalled ReasonText = "UnloadAppDomainCalled" Case Else ReasonText = "Unknown Reason - Wasn't Provided" End Select EndTime = Date.UtcNow If (Share IsNot Nothing) Then Share.Sessions.Dispose() Share.Sites.ShutDown(ReasonText, EndTime) Share.Logs("Activity-").WriteLine("Application_End: " & ReasonText) Share.Logs("Activity-").Flush() Else If (Trace Is Nothing) Then Trace = IO.File.AppendText(Hosting.HostingEnvironment.ApplicationPhysicalPath & TraceName) Trace.AutoFlush = True End If Trace.WriteLine("EndTime=" & EndTime.ToString) Trace.WriteLine("Ending Reason=" & ReasonText) End If For Each Item As Object In Me.Application Try Disposable = DirectCast(Item, IDisposable) Disposable.Dispose() Catch ex As Exception End Try Next Item Me.Application.RemoveAll() GC.KeepAlive(Me.Application) Catch ex As Exception If (Trace Is Nothing) Then Trace = IO.File.AppendText(Hosting.HostingEnvironment.ApplicationPhysicalPath & TraceName) Trace.AutoFlush = True End If Trace.WriteLine(Date.UtcNow.ToString("hh:mm:ss.ff tt ")) Trace.WriteLine(ex.Source) Trace.WriteLine(ex.Message) If (ex.StackTrace IsNot Nothing) Then Trace.WriteLine(ex.StackTrace) ex = ex.InnerException Do While (ex IsNot Nothing) Trace.WriteLine(ex.Source) Trace.WriteLine(ex.Message) If (ex.StackTrace IsNot Nothing) Then Trace.WriteLine(ex.StackTrace) ex = ex.InnerException Loop Finally GC.ReRegisterForFinalize(Me) If (Trace IsNot Nothing) Then Trace.Close() End Try End Sub Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs when an unhandled error occurs ' Careful if you have custom errors processing you do not want to clear the error. Dim Share As ShareClass = Nothing Dim LastError As Exception Dim Trace As IO.StreamWriter = Nothing Dim TraceName As String TraceName = "Logs\ApplicationError-" & Date.UtcNow.Date.ToString("yyyy-MM-dd") & ".log" Try Try Share = DirectCast(Me.Application("Shared"), ShareClass) Catch ex As Exception Trace = IO.File.AppendText(Hosting.HostingEnvironment.ApplicationPhysicalPath & TraceName) Trace.AutoFlush = True Trace.WriteLine(Date.UtcNow.ToString("hh:mm:ss.ff tt ")) Trace.WriteLine(ex.Source) Trace.WriteLine(ex.Message) If (ex.StackTrace IsNot Nothing) Then Trace.WriteLine(ex.StackTrace) ex = ex.InnerException Do While (ex IsNot Nothing) Trace.WriteLine(ex.Source) Trace.WriteLine(ex.Message) If (ex.StackTrace IsNot Nothing) Then Trace.WriteLine(ex.StackTrace) ex = ex.InnerException Loop End Try LastError = Me.Server.GetLastError If (LastError Is Nothing) Then If (Share IsNot Nothing) Then Share.Logs("Unhandled-").WriteLine("Application Error with no Error found.") Share.Logs("Unhandled-").Flush() Else If (Trace Is Nothing) Then Trace = IO.File.AppendText(Hosting.HostingEnvironment.ApplicationPhysicalPath & TraceName) Trace.AutoFlush = True End If Trace.WriteLine(Date.UtcNow.ToString("hh:mm:ss.ff tt ")) Trace.WriteLine("Application Error with no Error found.") End If ElseIf (Not LastError.Message.StartsWith("The file '")) _ OrElse (Not LastError.Message.EndsWith("' does not exist.")) Then If (Share IsNot Nothing) Then If (TypeOf LastError Is Data.SqlClient.SqlException) Then If (Share IsNot Nothing) Then Share.Logs("Data-").WriteSQLException(DirectCast(LastError, Data.SqlClient.SqlException)) Share.Logs("Data-").Flush() End If End If Share.Logs("Unhandled-").WriteException(LastError) Share.Logs("Unhandled-").Flush() Else If (Trace Is Nothing) Then Trace = IO.File.AppendText(Hosting.HostingEnvironment.ApplicationPhysicalPath & TraceName) Trace.AutoFlush = True End If Trace.WriteLine(Date.UtcNow.ToString("hh:mm:ss.ff tt ")) Trace.WriteLine(LastError.Source) Trace.WriteLine(LastError.Message) If (LastError.StackTrace IsNot Nothing) Then Trace.WriteLine(LastError.StackTrace) Dim ex As Exception ex = LastError.InnerException Do While (ex IsNot Nothing) Trace.WriteLine(ex.Source) Trace.WriteLine(ex.Message) If (ex.StackTrace IsNot Nothing) Then Trace.WriteLine(ex.StackTrace) ex = ex.InnerException Loop End If End If 'Me.Server.ClearError() 'Use when you don't have a custom error and don't want to display ASP.NET Catch ex As Exception If (Trace Is Nothing) Then Trace = IO.File.AppendText(Hosting.HostingEnvironment.ApplicationPhysicalPath & TraceName) Trace.AutoFlush = True End If Trace.WriteLine(Date.UtcNow.ToString("hh:mm:ss.ff tt ")) Trace.WriteLine(ex.Source) Trace.WriteLine(ex.Message) If (ex.StackTrace IsNot Nothing) Then Trace.WriteLine(ex.StackTrace) ex = ex.InnerException Do While (ex IsNot Nothing) Trace.WriteLine(ex.Source) Trace.WriteLine(ex.Message) If (ex.StackTrace IsNot Nothing) Then Trace.WriteLine(ex.StackTrace) ex = ex.InnerException Loop Finally If (Trace IsNot Nothing) Then Trace.Close() End Try End Sub Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs when a new session is started Dim Share As ShareClass Dim ThisSession As ISessionInterface Dim Browser As BrowserClass = Nothing Dim FiltersOut() As String = {} Dim IP As IPClass Dim Site As SiteClass Dim Domain As String Dim Request As RequestClass Share = DirectCast(Me.Application("Shared"), ShareClass) Share.Logs("Activity-").WriteLine("Session_Start") Share.Logs("Activity-").Flush() Domain = Hosting.HostingEnvironment.SiteName.Trim.ToLower Domain = Share.DomainTool.GetPrimaryDomain(Domain) Site = Share.Sites.FindSite(Domain) 'Set up custom errors in both web.config & at iis hosting account to capture Page Not Found(s) (404) & other 'request (Don't include a robots.txt file in root directory but use code in error page to determine if 'requested, this lets me know if this request is from a crawler/spider) do respond with a robots.txt file 'content that resides in another directory 'Can use Response object to determine status code If (Site Is Nothing) Then Try 'RobotsResponseFile = Nothing 'HighestSessionsLive = 0I 'SessionsTimeSpan = 15S 'SiteActivityMax = 91S 'SessionsNoDaysTillHourly = 31S 'SessionsNoDaysTillDaily = 91S 'SessionsNoDaysTillErase = 732S 'SessionTimeOut = Me.Session.Timeout 'ScriptTimeOut = Me.Server.ScriptTimeout 'UserNo Site = Share.Sites.AddSite(Domain, "~/Robots/robots.txt", 0I, 15S, 91S, 31S, 91S, 732S, _ Me.Session.Timeout, Me.Server.ScriptTimeout, 0I) Catch dex As Data.SqlClient.SqlException Share.Logs("Data-").WriteSQLException(dex) Share.Logs("Data-").Flush() Exit Sub Catch ex As Exception Share.Logs("Errors-").WriteException(ex) Share.Logs("Errors-").Flush() Exit Sub End Try End If If (Not Site.ActiveSessions.HasValue) Then Site.ActivateSite(Me.Server, Me.Session) If Share.IsIPBlocked(Me.Request.UserHostAddress) OrElse Share.IsDomainBlocked(Me.Request.UserHostName) _ OrElse Share.IsAgentBlocked(Me.Request.UserAgent) Then Share.Logs("Activity-").WriteLine("Session_Start: IP, Domain or Agent is application blocked.") Share.Logs("Activity-").Flush() Me.Response.Close() Exit Sub Else Try Request = New RequestClass(Me.Request, Site.Domain, FiltersOut) IP = Share.IPs.CheckIP(Request) Browser = Share.Browsers.GetBrowser(Request) Catch dex As Data.SqlClient.SqlException Share.Logs("Data-").WriteSQLException(dex) Share.Logs("Data-").Flush() Exit Sub Catch ex As Exception Share.Logs("Errors-").WriteException(ex) Share.Logs("Errors-").Flush() Exit Sub End Try If IP.IPBlock OrElse IP.DomainBlock OrElse Browser.Block Then Dim Blocks As String = "" If IP.IPBlock Then Blocks = "IP" Share.AddIPBlock(IP.IPAddress) End If If IP.DomainBlock Then If Blocks = "" Then Blocks = "Domain" Else Blocks &= ", Domain" End If Share.AddDomainBlock(IP.Domain) End If If Browser.Block Then If Blocks = "" Then Blocks = "User Agenet" Else Blocks &= ", User Agent" End If Share.AddAgentBlock(Me.Request.UserAgent) End If Share.Logs("Activity-").WriteLine("Session_Start: " & Blocks & " is blocked.") Share.Logs("Activity-").Flush() Me.Response.Close() Exit Sub End If End If 'Have IP, Site, Browser, Request Try ThisSession = Share.Sessions.AddSession(Request, Site, Browser, IP, Me.Session, Me.Response) Me.Session.Add("ThisSession", ThisSession) Catch ex As Exception Share.Logs("Errors-").WriteException(ex) Share.Logs("Errors-").Flush() End Try End Sub Sub Session_End(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs when a session ends. ' Note: The Session_End event is raised only when the sessionstate mode ' is set to InProc in the Web.config file. If session mode is set to StateServer ' or SQLServer, the event is not raised. Dim Disposable As IDisposable Dim Share As ShareClass Dim ThisSession As SessionClass Share = DirectCast(Me.Application("Shared"), ShareClass) ThisSession = DirectCast(Me.Session("ThisSession"), SessionClass) If ((ThisSession IsNot Nothing) AndAlso Share.Sessions.KillSession(ThisSession)) _ OrElse (ThisSession Is Nothing) Then For Each Item As Object In Me.Session Try Disposable = DirectCast(Item, IDisposable) Disposable.Dispose() Catch ex As Exception End Try Next Item End If Share.Logs("Activity-").WriteLine("Session_End") Share.Logs("Activity-").Flush() End Sub Protected Sub Application_PostAcquireRequestState(ByVal sender As Object, ByVal e As EventArgs) 'Global.asax doesn't capture requests for non-asp files (except with IIS 404 capture set) but httpmodules do. 'This is supposed to be after SessionState is acquired, but it isn't always! 'Execution Order: 'Session_Start 'Application_PostAcquireRequestState (This Sub) 'Page/Request Processing Dim Share As ShareClass Dim ThisSession As SessionClass If (Me.Context.Session Is Nothing) Then Exit Sub 'ASP.NET Development Server creates problems sometimes. ' I Don't know about IIS, (Undocumented extra processing) If (Not Me.Session.IsNewSession) Then ThisSession = DirectCast(Me.Session("ThisSession"), SessionClass) If (ThisSession Is Nothing) Then 'This usually means we are reusing a session that was ended. Share = DirectCast(Me.Application("Shared"), ShareClass) Share.Logs("Errors-").WriteLine("Application_PostAcquireRequestState Missing ThisSession.") Share.Logs("Errors-").Flush() 'ThisSession.Activity(Me.Request) is going to generate an Exception Else ThisSession.Activity(Me.Context) End If End If End Sub Protected Sub unused_Application_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs) 'This is just before a handler execute (Web page or other resource) 'We should have at this point everthing necessary to process request 'We should be able to handle all the activity methods from here but choose to use PostAcquireRequestState 'which is when we normally have the sessionstate. End Sub Protected Sub unused_Application_PreSendRequestHeaders(ByVal sender As Object, ByVal e As EventArgs) 'If we want to capture & replace ASP.NET 404 responses End Sub Protected Sub unused_Application_PreSendRequestContent(ByVal sender As Object, ByVal e As EventArgs) 'If we want to capture & replace ASP.NET 404 responses End Sub Private Function CreateSession(ByVal Request As RequestClass, ByVal Share As ShareClass, _ ByVal Site As SiteClass, ByVal Browser As BrowserClass, ByVal IP As IPClass, ByVal SessionId As String, _ ByVal Response As HttpResponse) As ISessionInterface CreateSession = New SessionClass(Request, Share, Site, Browser, IP, SessionId, Response) End Function Protected Sub unused_Application_EndRequest(ByVal sender As Object, ByVal e As EventArgs) Dim Share As ShareClass = DirectCast(Me.Application("Shared"), ShareClass) End Sub Protected Sub unused_Application_Disposed(ByVal sender As Object, ByVal e As EventArgs) Dim Share As ShareClass = DirectCast(Me.Application("Shared"), ShareClass) End Sub </script>