如何获取部署在 Azure 网站上的 ASP.NET 5 应用程序的错误详细信息?

How to Get Error Details of an ASP.NET 5 app deployed on Azure Websites?

我有一个 ASP.NET 5 解决方案,带有一个网站和几个项目库。我正在使用 MVC 6 和 Entity Framework 7。该应用程序在本地运行良好,直到今天它还在作为 Azure 网站部署的 Azure 上运行。

但是今天在 Azure 上进行最新部署后,我在启动时遇到了这样的错误 500(在本地仍然运行良好):

我试图通过以下方式获取更多详细信息:

error/exception 似乎是在 Startup/Configure 步骤中发生的,但我仍然收到没有详细信息的一般错误页面。即使是在服务器上生成的版本(DetailedErrors 文件夹)我也得到了这个:

我启用了失败请求跟踪,但仍然没有有用的信息:

即使我删除 Startup/Configure 中的代码并按照建议添加 try/catch,我也得到了相同的错误,但没有详细说明。这似乎是一个 configuration/compilation 问题,但没有任何信息很难调试。

您检查过 eventlog.xml 文件了吗?它在 D:\home\LogFiles 目录中。您可以从应用程序的 Kudu 站点查看它,或使用 Azure 网站事件查看器扩展。

当 运行 应用程序在 Azure 中(至少在 beta 3 中)时,ASPNET5 应用程序启动时发生的错误真的很难追踪。希望他们能找到改善体验的方法。我不得不求助于将我的启动程序剥离到最基本的结构,然后逐行添加代码直到失败发生(在我的例子中,这是一个缺少的环境变量)。

我也使用过这样的代码(仅用于调试),这可能会有所帮助,具体取决于错误发生的位置:

public void Configure(IApplicationBuilder app, IHostingEnvironment env )
    {           
        try
        {                       
            // Add MVC to the request pipeline.
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action}/{id?}"
                   );
            });
        }

//exceptions in startup are really bad when running in azure, all you will get is an internal server error
//this code will write the exception message to the browser instead.  Only use for debugging!!!

      catch (Exception ex)          
      {
            app.Run(async context =>
            {
                context.Response.ContentType = "text/plain";
                await context.Response.WriteAsync(ex.Message);
            });
        }
    }

2016 年 10 月 27 日更新 自从我最初的回答以来,发生了很多变化。最新指南张贴在这里:

https://docs.asp.net/en/latest/fundamentals/hosting.html

所以,添加:

.CaptureStartupErrors(true) 和 .UseSetting(WebHostDefaults.DetailedErrorsKey, "true") 在你的 WebHostBuilder 上,像这样:

 var host = new WebHostBuilder()
            .CaptureStartupErrors(true)
            .UseSetting(WebHostDefaults.DetailedErrorsKey, "true")
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

在您的 wwwroot 文件夹中创建一个 web.config,内容如下:

<configuration>
  <system.web>
    <customErrors mode="Off" />
  </system.web>
</configuration>

您是否尝试过使用 远程调试 Azure Webapp?很可能发生了一些异常,如果您观察 DEBUG OUTPUT window,您可能会看到发生了哪个异常,然后更改 Visual Studio 设置以中断该异常看看它发生在哪里。查看本文以了解如何远程调试 - http://blogs.msdn.com/b/webdev/archive/2013/11/05/remote-debugging-a-window-azure-web-site-with-visual-studio-2013.aspx

我遇到了同样的问题并花了很多时间试图深入研究错误日志等(上面给出的所有其他解决方案)。 None 他们给出了任何问题的线索。

我所做的帮助我最终看到错误的是简单地尝试 publish to a local IIS(毕竟 azure web-app 在 IIS 内部运行你的 dnx)。

当 IIS 尝试编译源代码时,我可以立即看到有错误。 (在我的例子中是一些格式错误的 nuget 包)。

简而言之:

通过发布到本地 IIS 重新创建 azure web-app 上发生的事情。

您必须在 [= 中将应用设置 属性 ASPNET_DETAILED_ERRORS 设置为 true 21=] 文件.

我编辑的 web.config 文件示例:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="bootstrapper-version" value="1.0.0-beta6" />
    <add key="runtime-path" value="..\approot\runtimes" />
    <add key="dnx-version" value="1.0.0-beta6" />
    <add key="dnx-clr" value="clr" />
    <add key="dnx-app-base" value="..\approot\src\MyApp" />
    <!-- This will turn on detailed errors when deployed to remote servers -->
    <!-- This setting is not recommended for production -->
    <add key="ASPNET_DETAILED_ERRORS" value="true" />
  </appSettings>
  <system.web>
    <httpRuntime targetFramework="4.5.1" />
  </system.web>
</configuration>

在我使用 beta5 的情况下,web.config 中的自定义错误没有帮助,本地 IIS 没问题,添加异常处理程序也没有显示任何内容。唯一有用的是核对 approot 并重新部署。

在 RC1 中(也许从 beta8 开始),显然应该使用:

app.UseDeveloperExceptionPage();

.. 这显然只有在 app.Properties["host.AppMode"]"development" 时才有效。

但这对我不起作用。我收到的错误消息特别是 "An error occurred while starting the application",我发现给定配置的 none 将解决此问题,因为错误发生在配置执行之前。

不知何故,发布目标文件夹一定是在发布过程中损坏了,因为我发现删除整个部署目录并重新发布解决了问题

否则参考这里:http://docs.asp.net/en/latest/fundamentals/diagnostics.html https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling

我在网络应用程序 运行 dnx-clr-win-x64.1.0.0-rc1-update1 中遇到了完全相同的错误。我直接从 Visual Studio 2015 Enterprise Update 1 进行了部署。每当我在新创建的 Web 应用程序上进行首次部署时,我发现该站点正在运行。从第二次部署开始(即使部署完全相同的内容),我开始看到内部服务器错误 500。这让我想到了以下解决方案:

在 Visual Studio 的发布向导中启用 "Remove additional files at destination" 为我修复了它。

在 Web 应用程序的应用程序设置中,在应用程序设置部分,将 Hosting:Environment 添加(或更改值)到 Development.然后你会得到与本地开发相同的错误页面。在我的 Startup.cs 中,我使用带有以下代码的常规 Configure() 方法:(仍在 MVC 1.0 RC1-final 中)

        if (env.IsDevelopment())
        {
            app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else

希望对您有所帮助!