Show ASP.NET 5 error page in Azure web app

依然范特西╮ 提交于 2019-11-28 21:13:22

In case this helps someone, I've found out that if you're using ASP.NET RC1 and you're using Azure WebApps and add an App setting called Hosting:Environment with a value of development a stack trace for server 500 errors will display.

For this to work the Configure method in the Startup.cs needs to use the Developer Exception page when you're using the Development environment. Eg:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
    }

    // etc
}

According to this post (thanks Muhammad), I should be able to get the runtime error on Azure by editing the server's web.config (quite correct, Celt).

Unfortunately this did not work - no detailed exception.

I did some digging around and found these "DetailedError" logs:

This is what they contained:

It appears that something may have been going wrong when trying to resolve favicon.ico at D:\home\site\wwwroot\favicon.ico.

There indeed was no favicon at that location. I rectified this, but still the same problem. In fact, I have never had a favicon, and this used to work.

In the end, I deleted the entire Web App in Azure Portal and republished... TADA, it works again.

When you get an HTTP 500 from an Azure Web Application that's running ASP.NET 5 and you can't get a detailed error output, in my experience it's for one of two reasons:

  • Your Startup.cs is causing the problem
  • The runtime cannot be loaded

For dealing with the first type of issue, you're best off writing an error handler that will log startup errors somewhere (we use Raygun.io for that, your needs and preferences should determine your solution).

For the second kind, the best I've come up with is through the Diagnostics feature of Web Sites -- you can access the Windows Server Event Logs, which will tell you if your runtime is borked.

Try setting your customErrors mode to off in your Web.config file like this:

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

Your startup.cs has some runtime code that isn't playing nice with Azure. Force the developer friendly exception page to render by moving app.UseDeveloperExceptionPage(); to the beginning of the Configure(...). Publish updated code to Azure, reload home page and exception will now be helpful.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
     {
          app.UseDeveloperExceptionPage();
      ...
     }

I had exactly the same problems - always getting 500 and never seen a tiniest log or information what was wrong. Even after commenting out everything in Startup.cs and simply accessing static files I was getting 500 (though surprisingly sometimes the static file was served correctly, it changed from publish to publish of the same app).

I suppose that some file(s) got corrupted in my deployment and azure wasn't detecting it. Or maybe there were some files left on the server that caused conflicts during runtime - next time I think it would be also worth to change publish profile to not keep extra files on server (by default they are not deleted).

I ended up removing and re-creating the app, which solved the problem.

If you're still on old versions of the DNX stack like myself (using beta5, which is what shipped with Visual Studio 2015), they had a setting that wasn't really documented well. I believe this has been changed since then, but here's what you had to place in your web.config:

  <appSettings>
    <add key="ASPNET_DETAILED_ERRORS" value="true" />
  </appSettings>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!