Knowledge Essentials - 3Essentials Hosting

Logging ASP.NET Application Restarts (including DNN)

Article ID: 1471

 Back to Search

reprinted from:
http://www.mitchelsellers.com/blogs/articletype/articleview/articleid/39/logging-aspnet-application-restarts.aspx


Logging ASP.NET Application Restarts 

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.

 
Downloads Associated With This Article
No downloads are currently associated with this article.