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 losing 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.