Azure上的Web Api使用'return InternalServerError(ex)'显示没有错误详细信息

时间:2014-08-21 15:20:09

标签: azure asp.net-web-api exception-handling

我在本地运行时的Web Api(在发布模式下)将以此格式返回任何错误:

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

但是在部署/发布到Azure VM之后,只剩下这个:

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

API代码:

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

我希望它在Azure上更详细,就像本地结果一样 这可以实现,如果是,如何实现?

我已经(暂时)从Web.Release.config的<compilation xdt:Transform="RemoveAttributes(debug)" />部分中删除<system.web>,然后重新部署,但这没有任何区别。

或者我使用了错误的方法/模式? 显然技术细节应该是有限的,但是现在我们根本没有得到任何细节。

5 个答案:

答案 0 :(得分:23)

您可以尝试将以下内容添加到Global.asax:

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

注意:我不建议您在生产环境中保持此设置。

答案 1 :(得分:4)

如果您使用

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Default; 

然后你可以使用system.webServer错误开关,例如

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

请注意existingResponse属性以保留错误消息。

答案 2 :(得分:2)

我遇到了同样的问题,这个帖子已经三年了,事情变了一点。如果使用Visual Studio 2017设置新的Azure移动应用程序,则不再有Global.asax.cs。我搜索了几个小时,在哪里放置这个IncludeErrorDetailPolicy。没有这种设置,它将无法运作。

您可以在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);

不要忘记,在你的Web.config中你还需要设置:

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

请勿将其用于生产环境!

答案 3 :(得分:1)

对于Web API 2,您可以实现使用Azure Application Insights的自定义IExceptionLogger。像这样:

CREATE TABLE "location"
  (
    zip         NUMBER (38) NOT NULL PRIMARY KEY,
    city        VARCHAR2 (30 BYTE) NOT NULL,
    state_symbol VARCHAR2 (30 BYTE) NOT NULL ,
    timezone    VARCHAR2 (30 BYTE) NOT NULL ,
    latitude    FLOAT ,
    longitude   FLOAT ,
    population  NUMBER (38) NOT NULL ,
    FOREIGN KEY (state_symbol) references "state"(symbol)
  ) ;

然后您需要使用Web API注册它:

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

为此,您需要在Azure中为您的VS项目设置Application Insight,但这是另一个时间的故事:) 有关详细信息,请参阅Application Insights: Exception Telemetry

答案 4 :(得分:0)

我有一个具有相同错误的方案,问题是方法的路径标头属性中的复制和粘贴。我有两种方法的相同路线

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

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

检查新方法和添加的路线。