Is it possible to catch an recycle event in the global.asax?
I know Application_End will be triggered but is there a way to know that it was triggered by a recycle of the application pool?
thx, Lieven Cardoen aka Johlero
Is it possible to catch an recycle event in the global.asax?
I know Application_End will be triggered but is there a way to know that it was triggered by a recycle of the application pool?
thx, Lieven Cardoen aka Johlero
So, here is an idea how this could work.
Based on my previous answer (attach to AppDomain.CurrentDomain.ProcessExit) and stephbu's comment:
I suggest following strategy:
In the (regular) ProcessExit handler (which we suppose will not be called on a application pool recycling), write some file to disk like "
app_domain_end_ok.tmp
".Then in the Application_Start of your global.asax check for this file. If it doesn't exist it is a sign that the application was not terminated in a clean way (or that it is the first time ever it started). Don't forget to delete this file from disk after the check.
I didn't try that myself, but it could be worth a try.
I found this article on Scott Guthries's blog:
Logging ASP.NET Application Shutdown Events
I was much more successful with attaching to DomainUnload event, it is triggered on AppPool recycle and stoppage of the AppPool itself.
AppDomain.CurrentDomain.DomainUnload += this.CurrentDomainOnProcessExit;
I've never tried this myself, but you could try to attach an event handler to the ProcessExit event of the AppDomain.
I hope this helps!