Logging ASP.NET Application Restarts 

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:

//  obtain the shutdown reason
System.Web.ApplicationShutdownReason shutdownReason System.Web.Hosting.HostingEnvironment.ShutdownReason;
string 
shutdownDetail "";

//Evaluate which option caused the error
switch (shutdownReason)
{
    
case ApplicationShutdownReason.BinDirChangeOrDirectoryRename:
        shutdownDetail 
"A change was made to the bin directory or the directory was renamed";
        break;
    case 
ApplicationShutdownReason.BrowsersDirChangeOrDirectoryRename:
        shutdownDetail 
"A change was made to the App_browsers folder or the files contained in it";
        break;
    case 
ApplicationShutdownReason.ChangeInGlobalAsax:
        shutdownDetail 
"A change was made in the global.asax file";
        break;
    case 
ApplicationShutdownReason.ChangeInSecurityPolicyFile:
        shutdownDetail 
"A change was made in the code access security policy file";
        break;
    case 
ApplicationShutdownReason.CodeDirChangeOrDirectoryRename:
        shutdownDetail 
"A change was made in the App_Code folder or the files contained in it";
        break;
    case 
ApplicationShutdownReason.ConfigurationChange:
        shutdownDetail 
"A change was made to the application level configuration";
        break;
    case 
ApplicationShutdownReason.HostingEnvironment:
        shutdownDetail 
"The hosting environment shut down the application";
        break;
    case 
ApplicationShutdownReason.HttpRuntimeClose:
        shutdownDetail 
"A call to Close() was requested";
        break;
    case 
ApplicationShutdownReason.IdleTimeout:
        shutdownDetail 
"The idle time limit was reached";
        break;
    case 
ApplicationShutdownReason.InitializationError:
        shutdownDetail 
"An error in the initialization of the AppDomain";
        break;
    case 
ApplicationShutdownReason.MaxRecompilationsReached:
        shutdownDetail 
"The maximum number of dynamic recompiles of a resource limit was reached";
        break;
    case 
ApplicationShutdownReason.PhysicalApplicationPathChanged:
        shutdownDetail 
"A change was made to the physical path to the application";
        break;
    case 
ApplicationShutdownReason.ResourcesDirChangeOrDirectoryRename:
        shutdownDetail 
"A change was made to the App_GlobalResources foldr or the files contained within it";
        break;
    case 
ApplicationShutdownReason.UnloadAppDomainCalled:
        shutdownDetail 
"A call to UnloadAppDomain() was completed";
        break;
    default
:
        shutdownDetail 
"Unknown shutdown reason";
        break;
}

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.

Posted by Mitchel on Thursday, March 15, 2007
 

Comments

Here ya' go Mitchel, a VB version...

' obtain the shutdown reason
Dim shutdownReason As System.Web.ApplicationShutdownReason = System.Web.Hosting.HostingEnvironment.ShutdownReason
Dim shutdownDetail As String = ""

'Evaluate which option caused the error
Select 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 Select
End Select

By Ed DeGagne on Wednesday, August 08, 2007 at 6:38 PM

Just remove the "Exit Select" statements.

By Ed DeGagne on Wednesday, August 08, 2007 at 6:39 PM

Great and very valueable information.

Shouldn't this be posted in the log viewer?

By Lance Long on Tuesday, September 25, 2007 at 2:27 PM
Click here to post a comment

Donate

Show your appreciation for the content/modules made available by MitchelSellers.com by making a donation. Donations are used to assist with dedicating time to creating free content.