Web Api on Azure does not show error details using 'return InternalServerError (ex)'

My Web Api, when launched locally (in Release mode), will return any errors in this format:

{ "Message": "An error has occurred.", "ExceptionMessage": "No text specified", "ExceptionType": "System.Exception", "StackTrace": null } 

But after deploying / publishing to Azure VM, only the following remains:

 { "Message": "An error has occurred." } 

API Code:

 try { var msg = ... new MessageService().SaveMessage(msg)); // <-- does some checks; may throw. return Ok(); } catch (Exception ex) { return InternalServerError(ex); } 

I would like this to be more detailed on Azure as a local result.
Could this be achieved, and if so, how?

I already (temporarily) deleted <compilation xdt:Transform="RemoveAttributes(debug)" /> from the <system.web> part of Web.Release.config, and then redeployed, but that didn't matter.

Or am I using the wrong approach / pattern?
Obviously, technical details should be limited, but right now we are not getting any details.

+8
exception-handling asp.net-web-api azure
source share
5 answers

You can try adding the following to your Global.asax file:

 GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; 

Note. I would not recommend that this option be enabled in a production environment.

+20
source share

If instead you use

 GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Default; 

then you can use the system.webServer error switch, for example.

 <system.webServer> <httpErrors errorMode="Detailed" existingResponse="PassThrough"> </httpErrors> </system.webServer> 

Pay attention to the existingResponse attribute to save the error message.

+4
source share

For Web API 2, you can implement a custom IExceptionLogger that uses Azure Application Insights. Something like that:

 using Microsoft.ApplicationInsights; using System.Web.Http.ExceptionHandling; namespace Your.Namespace.Here { public class TelemetryExceptionLogger : ExceptionLogger { private readonly TelemetryClient telemetryClient; public TelemetryExceptionLogger(TelemetryClient telemetryClient) { this.telemetryClient = telemetryClient; } public override void Log(ExceptionLoggerContext context) { if (context != null && context.Exception != null) { telemetryClient.TrackException(context.Exception); } base.Log(context); } } } 

Then you need to register it using the Web API:

 using Microsoft.ApplicationInsights; using System.Web.Http; using System.Web.Http.ExceptionHandling; using Your.Namespace.Here; namespace Some.Other.Namespace.Or.The.Same { public static class WebApiConfig { public static void Register(HttpConfiguration config) { // --- Normal Web API configuration here --- config.Services.Add(typeof(IExceptionLogger), new TelemetryExceptionLogger(new TelemetryClient())); } } } 

To do this, you will need to configure Application Insight in Azure and for your VS project, but this is a story at a different time :) For more information, see Application Insights: Exception Telemetry

+1
source share

I had the same problem that the position was three years, everything has changed a bit. If you configure the new Azure Mobile app with Visual Studio 2017, there will no longer be Global.asax.cs. I was looking for a watch where you can put this IncludeErrorDetailPolicy. It will not work without this setting.

You do this in your Startup.MobileApp.cs:

  public partial class Startup { public static void ConfigureMobileApp(IAppBuilder app) { HttpConfiguration config = new HttpConfiguration(); config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; new MobileAppConfiguration() .UseDefaultConfiguration() .ApplyTo(config); 

Do not forget that in your Web.config you also need to install:

 <system.webServer> <httpErrors errorMode="Detailed" existingResponse="PassThrough"> </httpErrors> </system.webServer> 

Do not use this for a working environment!

+1
source share

I have a script with the same error, and the problem was copying and pasting the attribute of the method route header. I have the same route for two methods

  [Route("test/Method1")] public IHttpActionResult Method1(){...} [Route("test/Method1")] public IHttpActionResult Method2(){...} 

Check out the new methods and added routes.

0
source share

All Articles