如果无法访问事件日志(因为您处于共享托管环境中),您将获得的最多信息来自 Application_End 事件并通过向 HttpRuntime(通过反射)询问值遗憾的是没有公开曝光的一两个私人成员。
为此,请将以下代码添加到您的 Application_End 事件中:
BindingFlags staticFlags =
BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField;
BindingFlags instanceFlags =
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField;
HttpRuntime runtime = (HttpRuntime)typeof(System.Web.HttpRuntime)
.InvokeMember("_theRuntime", staticFlags, null, null, null);
if(runtime != null)
{
string shutDownMessage = (string)runtime.GetType()
.InvokeMember("_shutDownMessage", instanceFlags, null, runtime, null);
string shutDownStack = (string)runtime.GetType()
.InvokeMember("_shutDownStack", instanceFlags, null, runtime, null);
// Log shutDownMessage & shutDownStack somewhere
}
如果我关闭或回收应用程序的应用程序池,我会看到以下内容:
HostingEnvironment 启动关闭
HostingEnvironment 导致关机 -
在 System.Environment.GetStackTrace(异常 e,布尔需要文件信息)
在 System.Environment.get_StackTrace()
在 System.Web.Hosting.HostingEnvironment.InitiateShutdownInternal()
在 System.Web.Hosting.HostingEnvironment.InitiateShutdownWithoutDemand()
在 System.Web.Hosting.PipelineRuntime.StopProcessing()
这可能已经差不多了。
更新:
我不记得在哪里找到了这段代码,但 Drew 提醒我这是来自 Scott Guthrie 的一篇博文。
还有其他一些可能有用的私有成员,例如:
private ApplicationShutdownReason _shutdownReason;
您可以在 .NET Reflector 中检查这些字段(如果您仍有未定时炸弹的副本)或替代方法之一 (Open Source Alternatives to Reflector?)。