Add the record method in the application_end event on the site. Add the following Code to the Global. asax. CS file:
Void application_end (Object sender, eventargs e) {// code that runs on application shutdown recordendreason ();} protected void recordendreason () {httpruntime runtime = (httpruntime) typeof (system. web. httpruntime ). invokemember ("_ theruntime", bindingflags. nonpublic | bindingflags. static | bindingflags. getfield, null); If (runtime = NULL) return; string shutdownmessage = (string) runtime. getType (). invokemember ("_ shutdownmessage", bindingflags. nonpublic | bindingflags. instance | bindingflags. getfield, null, runtime, null); string shutdownstack = (string) runtime. getType (). invokemember ("_ shutdownstack", bindingflags. nonpublic | bindingflags. instance | bindingflags. getfield, null, runtime, null); EventLog log = new EventLog (); log. source = "ASP. net 2.0.50727.0 "; log. writeentry (string. format ("\ r \ n \ r \ n_shutdownmessage = {0} \ r \ n \ r \ n_shutdownstack = {1}", shutdownmessage, shutdownstack), eventlogentrytype. information );}
the above method records the cause of the restart and the stack information during the restart to the Windows event viewer. Of course, you can also record the information in a text file.