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)
注意:我不建议您在生产环境中保持此设置.
如果你使用
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 属性以保留错误消息。