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

Pet*_*r B 10 exception-handling azure asp.net-web-api

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

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "No text specified",
    "ExceptionType": "System.Exception",
    "StackTrace": null
}
Run Code Online (Sandbox Code Playgroud)

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

{
    "Message": "An error has occurred."
}
Run Code Online (Sandbox Code Playgroud)

API代码:

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

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

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

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

Jon*_*iak 24

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

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
Run Code Online (Sandbox Code Playgroud)

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

  • 我已将其添加到我的web api中的Global.asax并发布到Azure,但我仍然只收到"发生错误".还有什么可能阻止返回真正的错误吗? (3认同)

Pau*_*her 5

如果你使用

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Default; 
Run Code Online (Sandbox Code Playgroud)

那么你可以使用 system.webServer 错误开关,例如

<system.webServer>
    <httpErrors errorMode="Detailed" existingResponse="PassThrough">
    </httpErrors>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

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