在global.asax中是否有可能捕获回收事件?
我知道应用程序的结束会被触发,但是有没有一种方法可以知道它是由应用程序池的回收触发的呢?
谢谢,Johlero(Lieven Cardoen)
在global.asax中是否有可能捕获回收事件?
我知道应用程序的结束会被触发,但是有没有一种方法可以知道它是由应用程序池的回收触发的呢?
谢谢,Johlero(Lieven Cardoen)
public void Application_End() {
HttpRuntime runtime =
(HttpRuntime) typeof(System.Web.HttpRuntime).InvokeMember("_theRuntime",
BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField,
null, null, 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);
if (!EventLog.SourceExists(".NET Runtime")) {
EventLog.CreateEventSource(".NET Runtime", "Application");
}
EventLog log = new EventLog();
log.Source = ".NET Runtime";
log.WriteEntry(String.Format(
"\r\n\r\n_shutDownMessage={0}\r\n\r\n_shutDownStack={1}",
shutDownMessage, shutDownStack),
EventLogEntryType.Error);
}
我建议采用以下策略:这将捕获大多数结构化进程拆除,例如-但我不确定它是否将捕获所有拆除。例如http://blogs.msdn.com/jmstall/archive/2006/11/26/process-exit-event.aspx如果进程似乎挂起,进程回收将终止进程-您的处理程序将不会被调用。
app_domain_end_ok.tmp
”。我自己从未尝试过,但你可以尝试将事件处理程序附加到AppDomain的ProcessExit事件。
...
AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnExit);
...
void OnExit(object sender, EventArgs e) {
// do something
}
希望这能有所帮助!
我在附加到DomainUnload事件方面更加成功,它会在AppPool回收和AppPool本身停止时触发。
AppDomain.CurrentDomain.DomainUnload += this.CurrentDomainOnProcessExit;