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

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


Errors that occur in startup in an ASPNET5 application are really hard to track down when running the app in Azure (at least with beta 3). Hopefully they find a way to improve the experience. I had to resort to stripping my startup down to the bare bones and then adding code line by line until the failure happened (in my case, it was a missing environment variable).

I've also used code like this (for debugging only) which might help depending on where the error is happening:

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

Update 10/27/2016 A lot has changed since my original answer. The latest guidance is posted here:

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

So, add:

.CaptureStartupErrors(true) and .UseSetting(WebHostDefaults.DetailedErrorsKey, "true") on your WebHostBuilder like so:

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


In RC1 (as of beta8, perhaps), one should apparently use:

app.UseDeveloperExceptionPage();

.. which apparently only works if app.Properties["host.AppMode"] is "development".

But this didn't work for me. The error message I was getting was specifically "An error occurred while starting the application", I have found that none of the given configurations will resolve this because the error is occurring before the configurations execute.

Somehow, the publish target folder must have gotten corrupted during publish because I found that deleting the entire deployment directory and re-publishing resolved the problem.

Otherwise, here is the reference: http://docs.asp.net/en/latest/fundamentals/diagnostics.htmlhttps://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling


You must set app settings property ASPNET_DETAILED_ERRORS to true in web.config file.

Example of my edited web.config file:

<?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>