网站搜索

javascript - Application Insights JS trackException 显示 "Failed method: 'Unknown'"


我正在尝试使用 AppInsights,并使用以下代码:(获取操作返回 500 错误)

private callMethod(): void {
  this._callMethodInternal();
}

private async _callMethodInternal(): Promise<void> {
  try {
    await fetch("https://localhost:44369/api/values", {
      method: "GET",
      headers: {
        Accept: "application/json",
        "Content-Type": "text/json"
      }
    });
  } catch (err) {
    AppInsights.trackException(err);

    throw err;
  }
}

异常显示在 Application Insights 中,但“失败方法”属性显示的值是“未知”。有没有办法显示正确的方法名称?

谢谢

(https://github.com/Microsoft/ApplicationInsights-JS/issues/680)

答案#1

你没有传递正确的参数。以下是更新的方法

trackException(exception: Error, handledAt?: string, properties?: {[string]:string}, measurements?: {[string]:number}, severityLevel?: AI.SeverityLevel)

供参考,请查看以下链接 https://github.com/microsoft/ApplicationInsights-JS/blob/master/API-reference.md

答案#2

trackException 期望的对象类型是 IExceptionTelemtry,而不是原生的 Error 对象。换句话说,你不能将捕获的错误直接传递给 trackException。

API 参考概述了所有 IExceptionTelemtry 属性,但最重要的是 exception 属性,因为它是必需的。

示例:

try {
  // logic that throws
} catch (error) {
  AppInsights.trackException({ exception: error });
}