Web Api on Azure shows no error detail using '

2019-04-23 18:07发布

My Web Api when run 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 deployment/publish to an Azure VM, only this 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'd like it to be more detailed on Azure, like the local result.
Can this be achieved, and if so, how?

I already (temporarily) removed <compilation xdt:Transform="RemoveAttributes(debug)" /> from the <system.web> part of Web.Release.config, and then re-deployed, but that made no difference.

Or am I using the wrong approach/pattern?
Obviously technical details should be limited, but right now we get no details at all.

5条回答
【Aperson】
2楼-- · 2019-04-23 18:42

I have a scenario with the same error, and the problem was a copy&paste in the route header attribute of a method. I have the same route for two methods

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

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

Check the new methods and Routes added.

查看更多
老娘就宠你
3楼-- · 2019-04-23 18:44

If instead you use

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Default; 

then you can use the system.webServer error switch e.g.

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

Note the existingResponse attribute to preserve the error message.

查看更多
孤傲高冷的网名
4楼-- · 2019-04-23 18:45

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

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

For this to work, you will need to have set up Application Insight in Azure and for your VS project, but that is a story for another time :) For more information, see Application Insights: Exception Telemetry

查看更多
【Aperson】
5楼-- · 2019-04-23 18:53

I had the same problem, the post is three years old, things have changed a little. If you setup a new Azure Mobile App with Visual Studio 2017 there is no longer a Global.asax.cs. I searched for hours, where to put this IncludeErrorDetailPolicy. It won't work without that setting.

You do it 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);

Don't forget, in your Web.config you also need to set:

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

Don't use that for production environment!

查看更多
做个烂人
6楼-- · 2019-04-23 18:55

You could try adding the following to your Global.asax:

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

Note: I wouldn't recommend that you keep this setting on in a production environment.

查看更多
登录 后发表回答