It is necessary to record the reason why the website stops running. The following is a specific implementation method.
Copy codeThe Code is as follows:
Protected void Application_End (object sender, EventArgs e)
{
RecordEndReason ();
}
/// <Summary>
/// Record the reason why the website stops running
/// </Summary>
Protected void RecordEndReason ()
{
HttpRuntime runtime = (HttpRuntime) typeof (System. web. httpRuntime ). invokeMember ("_ theRuntime", System. reflection. bindingFlags. nonPublic | System. reflection. bindingFlags. static | System. reflection. bindingFlags. getField,
Null,
Null,
Null );
If (runtime = null)
Return;
String shutDownMessage = (string) runtime. GetType (). InvokeMember ("_ shutDownMessage ",
System. Reflection. BindingFlags. NonPublic | System. Reflection. BindingFlags. Instance | System. Reflection. BindingFlags. GetField,
Null,
Runtime,
Null );
String shutDownStack = (string) runtime. GetType (). InvokeMember (
"_ ShutDownStack ",
System. Reflection. BindingFlags. NonPublic | System. Reflection. BindingFlags. Instance | System. Reflection. BindingFlags. GetField,
Null,
Runtime,
Null );
String reasonString = "website Application_End, stop running, shutDownMessage =" + shutDownMessage + ", shutDownStack =" + shutDownStack;
LogHelper. WriteErrorLog (reasonString, null );
LogHelper. WriteSmtp (reasonString, null );
// The following 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.
// 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 );
}
Effect