Blogs

Logging ASP.NET Application Restarts

15 Mar

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.

tags: DNN, ASP.NET, .NET 1.1, .NET 2.0
comments powered by Disqus

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.

Connect with Mitchel

I hope the information here has been helpful. To stay connected you can also subscribe to blog updates via email, contact Mitchel about consulting services, or reach out for assistance via CodeMendor

Content Copyright

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.