Like many ASP.NET programmers I utilize a third party hosting company to host all of my sites. We have all ran into situations where we have been loosing sessions and other oddities and it would be very helpful to find out exactly what caused the application to restart. Well thanks to this post on DotNetNuke.com by Frankt I have found a way to track this information. Below I provide you with code that is needed to obtain this information
The entire process is very simple it is a few simple lines of code placed in the "Application_End" method within the Global.asax file. The main portion of the code is the first line which retrieves the ApplicationShutdownReason enum value for the current shutdown from the System.Web.Hosting.HostingEnvironment class. Then the remainder of the code is a large switch statement to populate a "shutdownDetail" string object with a detailed message. Below is the code:
Now that you have your shutdownDetail you can do what you desire with it. In my application I am simply logging it to a database table for future reference, however you could just as easily e-mail it or write it to a log file. This will at least give you a better idea as to what is happening on your server. Please let me know if there any comments/questions.
For the DotNetNuke users, this is something that could be ported to VB and placed in the DNN core global.asax module if you would like to track restarts on a DNN install. If the demand is high enough I can port this over to VB.NET using a Select Case statement.
Here ya' go Mitchel, a VB version...' obtain the shutdown reasonDim shutdownReason As System.Web.ApplicationShutdownReason = System.Web.Hosting.HostingEnvironment.ShutdownReasonDim shutdownDetail As String = ""'Evaluate which option caused the errorSelect Case shutdownReason Case ApplicationShutdownReason.BinDirChangeOrDirectoryRename shutdownDetail = "A change was made to the bin directory or the directory was renamed" Exit Select Case ApplicationShutdownReason.BrowsersDirChangeOrDirectoryRename shutdownDetail = "A change was made to the App_browsers folder or the files contained in it" Exit Select Case ApplicationShutdownReason.ChangeInGlobalAsax shutdownDetail = "A change was made in the global.asax file" Exit Select Case ApplicationShutdownReason.ChangeInSecurityPolicyFile shutdownDetail = "A change was made in the code access security policy file" Exit Select Case ApplicationShutdownReason.CodeDirChangeOrDirectoryRename shutdownDetail = "A change was made in the App_Code folder or the files contained in it" Exit Select Case ApplicationShutdownReason.ConfigurationChange shutdownDetail = "A change was made to the application level configuration" Exit Select Case ApplicationShutdownReason.HostingEnvironment shutdownDetail = "The hosting environment shut down the application" Exit Select Case ApplicationShutdownReason.HttpRuntimeClose shutdownDetail = "A call to Close() was requested" Exit Select Case ApplicationShutdownReason.IdleTimeout shutdownDetail = "The idle time limit was reached" Exit Select Case ApplicationShutdownReason.InitializationError shutdownDetail = "An error in the initialization of the AppDomain" Exit Select Case ApplicationShutdownReason.MaxRecompilationsReached shutdownDetail = "The maximum number of dynamic recompiles of a resource limit was reached" Exit Select Case ApplicationShutdownReason.PhysicalApplicationPathChanged shutdownDetail = "A change was made to the physical path to the application" Exit Select Case ApplicationShutdownReason.ResourcesDirChangeOrDirectoryRename shutdownDetail = "A change was made to the App_GlobalResources foldr or the files contained within it" Exit Select Case ApplicationShutdownReason.UnloadAppDomainCalled shutdownDetail = "A call to UnloadAppDomain() was completed" Exit Select Case Else shutdownDetail = "Unknown shutdown reason" Exit SelectEnd Select
Just remove the "Exit Select" statements.
Great and very valueable information.Shouldn't this be posted in the log viewer?
Name (required)
Email (required)
Website
Notify me of followup comments via e-mail
Content provided in this blog is provided "AS-IS" and the information should be used at your own discretion. The thoughts and opinions expressed are the personal thoughts of Mitchel Sellers and do not reflect the opinions of his employer.
Subscribe To Blog RSSSubscribe To Blog Updates by E-Mail *Add to Technorati Favorites
Click here for advertising information.
Content in this blog is copyright protected. Re-publishing on other websites is allowed as long as proper credit and backlink to the article is provided. Any other re-publishing or distribution of this content is prohibited without written permission from Mitchel Sellers.