Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
IIS Recycle Global.asax_Iis_Events_Global Asax_Recycle - Fatal编程技术网

IIS Recycle Global.asax

IIS Recycle Global.asax,iis,events,global-asax,recycle,Iis,Events,Global Asax,Recycle,是否可以在global.asax中捕获回收事件 我知道应用程序_End将被触发,但有没有办法知道它是由应用程序池的循环触发的 thx,Lieven Cardoen又名Johlero我自己从未尝试过,但您可以尝试将事件处理程序附加到AppDomain的ProcessExit事件 ... AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnExit); ... void OnExit(object sender, EventArgs

是否可以在global.asax中捕获回收事件

我知道应用程序_End将被触发,但有没有办法知道它是由应用程序池的循环触发的


thx,Lieven Cardoen又名Johlero

我自己从未尝试过,但您可以尝试将事件处理程序附加到AppDomain的ProcessExit事件

...
AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnExit);
...

void OnExit(object sender, EventArgs e) {
    // do something
}

我希望这有帮助

所以,这里有一个想法,这是如何工作的

根据我的(附加到AppDomain.CurrentDomain.ProcessExit)和的注释:


这将捕获大多数结构化流程 撕裂,例如-但我不确定 它将捕获所有的撕裂。例如 进程回收将终止进程 如果它似乎被挂起-你的处理程序 不会接到电话的

我建议采取以下策略:

在(常规)ProcessExit处理程序中(我们假设在应用程序池回收时不会调用该处理程序),将一些文件写入磁盘,如“
app\u domain\u end\u ok.tmp

然后在应用程序中启动global.asax,检查此文件。如果它不存在,则表明应用程序没有以干净的方式终止(或者是首次启动)。检查后不要忘记从磁盘删除此文件


我自己没有试过,但值得一试

我在Scott Guthries的博客上找到了这篇文章:

最近有人在listserv上问 是否有办法找到答案 ASP.NET重新启动的原因和时间 应用程序域。具体来说,他 我在寻找死亡的确切原因 是什么在他身上触发了他们 在生产共享系统中的应用 托管环境(它是一个 web.config文件更改,一个global.asax 更改,应用程序代码目录更改, 目录删除更改, 最大编译数已达到配额, \bin目录更改等)

我队的托马斯很酷 他编写的使用 一些巧妙的私人反思技巧 捕获并记录此信息。 它很容易重用和添加 进入任何应用程序,并可以使用 将信息记录在任何您需要的地方 希望(下面的代码使用NT事件 登录以保存它–但您可以 轻松地将其发送到数据库或通过 电子邮件给管理员)。代码是有效的 使用ASP.NET V1.1和ASP.NET V2.0

只需添加系统。反射和 将System.Diagnostics命名空间添加到您的 Global.asax类/文件,然后添加 应用程序将使用此 代码:


我更成功地附加到DomainUnload事件,它是在AppPool回收和AppPool本身停止时触发的


AppDomain.CurrentDomain.DomainUnload+=this.CurrentDomainOnProcessExit

这将捕获大多数结构化流程分解,例如-但我不确定它是否会捕获所有分解。e、 g.如果进程似乎挂起,进程回收将终止进程-您的处理程序将不会被调用。应用程序回收可能是一件非常残酷的事情,具体取决于进程的状态-最糟糕的情况是调用Win32::TerminateProcess。在您的过程中,没有办法捕获这样的结果。你想达到什么目标?刷新某些状态?我还没有对此进行验证,但我怀疑process recycler有两种策略—1)在轮询进程退出时发出终止进程的信号并等待—在正常情况下,它应该触发ProcessExit。2) 等待终止PID超时。
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);
}