azure - Azure Functions 与 App Insights 中的错误报告

标签 azure azure-functions azure-application-insights

我们调用 Azure Functions 托管的基于 REST 的 API,但未能实现支持 App Insights 的一致错误处理,并且想知道可以采取什么措施:

  • 如果我们不处理函数的异常,则 App Insights 报告“失败”,但服务仅向调用者返回错误代码,但没有错误内容: 因此,客户端会收到 500,仅此而已。

  • 如果我们处理异常并将其记录(到 AppInsights),则 App Insights 将停止报告“故障”,因此功能级别的监控将被破坏。我们可以查询异常,但它们是脱离上下文的(即我们只能通过自定义查询看到异常),并且我们不知道哪个函数实际上受到影响。

如何将这两种需求结合起来:

  1. 让函数失败,以便 AppInsights 报告失败(并且监视器可以发出警报)
  2. 向调用者返回比 500 更有意义的错误消息。

AppInsights 中的外观示例:

Exception is visible on the Exceptions tab, but the underlying operation has not failed

更新: 根据 Microsoft 的说法,App Insight 故障仅针对未处理的异常。不过,请考虑是否有办法至少传递错误消息。

最佳答案

If don't handle exceptions of the function, then App Insights reports a 'failure', returns the error code, but no error content. Hence, the client receives a 500 and thats it.

App Insights 不会返回任何内容,那么返回错误代码是什么意思?

If we handle the exception, LOG it (to AppInsights), return simple error message in code 500, then App Insights doesn't log this as 'failure' hence monitoring is not possible.

你能展示一下你是如何进行日志记录的吗?因为一旦您使用 App Insights 记录异常,您就应该在失败下看到它。

这应该有效:

try
{
   ...
}
catch(Exception e)
{
   logger.LogError(e, e.Message);
   return httpRequest.CreateResponse(HttpStatusCode.InternalServerError, e.Message);
}

关于azure - Azure Functions 与 App Insights 中的错误报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69464243/

相关文章:

azure - Visual Studio 2022 缓存旧租户 ID,无法删除它

azure - 无法通过点到站点 VPN 连接连接到 azure SQL

c# - 如何在 Azure 中运行安全的 Service Fabric 有状态服务?

sql-server - Azure 函数中的 SQL 连接

Azure.Identity.AuthenticationFailedException : ManagedIdentityCredential authentication failed: Service request failed

azure - 处理事件时出现EventSourceException "LogVerbose"

azure - Application Insights 分析 让运算符(operator)

python - Azure 功能运行状况检查失败

node.js - Socket.io + Azure Web 套接字问题

c# - System.InvalidOperationException : 'Unable to resolve service for type ' Microsoft. AspNetCore.Hosting.IHostingEnvironment'