I'm trying to detect when an ASP.NET application is recycling due to either the web.config file being modified or the IIS application pool being manually recycled.
Initially I thought ASP.NET's Application_End method would work, and tried the following:
protected void Application_End(object sender, EventArgs e)
{
File.AppendAllText("log.txt", DateTime.Now + "\n");
}
The file was created the first time the web.config file was changed, but subsequent changes didn't fire the event. Similarly, when testing in IIS, the first manual application pool recycle created the file but later ones didn't - it's as if the Application_End event only ever fires once.
How would I detect each time the pool/app recycles?
The following might be a little bit of a hack but you could use the application Cache
to figure it out. Everytime a page loads you could check the cache for a specific key, if the key doesn't exist then you can consider it a 'recycle' and then add the key. Not the best method but might just work for what you need.
Eg. In your base page's Page_Load
or somewhere that will run with every request, you could do the following:
if (HttpContext.Current.Cache["RecycleCheck"] != null)
{
// Add flag to cache
HttpContext.Current.Cache.Insert("RecycleCheck", "1", null,
DateTime.UtcNow.AddDays(2), // 2 days (most will recycle by then)
System.Web.Caching.Cache.NoSlidingExpiration);
// Do what you need because a recycle has happened
}
This method will not pick it up as the recycle happens. It will only identify a recycle on the first request after the recycle.
The Application_Start
would be the most reliable place to do this but it suffers from the same issue as the hack with the fact that it happens after the recycle on the first request.