如何找出 Application_End 在 Azure 应用服务上触发的原因

How to find out why Application_End is firing on Azure App Service

我们有一个 MVC ASP.NET Web 应用程序作为应用服务托管在 Azure 上。 每天 3 到 4 次我可以看到它重新启动。我在 global.asax 中的 Application_End() 中记录 System.Web.HostingEnvrionment.ShutdownReason 并且返回的原因是 "Configuration Change",根据文档这意味着应用程序配置已更改。

我问过我们的小团队,没有人手动更改配置。搜索代码我没有看到我们以编程方式更改它的任何地方。天蓝色的站点配置为始终在线。发生这种情况时,内存使用量并未接近极限,但它似乎在较高流量期间发生得更频繁。

有没有办法获取更改的特定文件,以便我可以将其记录在 Application_End() 中?或任何其他获取更多详细信息的方法?

Scott Guthrie 有一篇博客 post 介绍如何使用反射获得更多 information out of the Application Shut Down events。我会详细阅读他的 post 以了解有关问题的更多信息。

从 Scott 页面提取更多信息的代码片段如下。然后您可以使用您正在使用的任何工具记录它。

 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);

评论中提到您可能需要对代码具有某些权限才能执行私有反射。

祝你好运,我希望这能帮助你更接近解决问题。