angular 通用中的应用洞察

Sha*_*ghi 6 azure-application-insights server-side-rendering angular-universal angular

我有一个 Angular 项目,我想使用Application Insights进行日志记录。
我试过这个教程,它工作得很好,但不是在 SSR 模式下

当我在 SSR 模式下运行应用程序时,我收到此错误:

RerefenceError: XmlHttpRequest is not defined
Run Code Online (Sandbox Code Playgroud)

我找到了这个SO post,这似乎与我的问题相同。按照它的一个答案,我已经安装了XMLHttpRequest库并将这一行添加到server.ts

(global as any).XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;    
Run Code Online (Sandbox Code Playgroud)

不为我工作。Angular Universal 尚未响应http 请求。另外,我尝试使用“xhr2”库而不是“xmlhttprequest”,它也不起作用。

如何获得应用程序洞察以在 Angular SSR 模式下工作?


更多详情

这就是我将应用程序洞察添加到我的项目的方式。

monitoring.service.tsaiAPI的包装器:

RerefenceError: XmlHttpRequest is not defined
Run Code Online (Sandbox Code Playgroud)

然后像这样调用aiat的初始化代码app.component.ts

(global as any).XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;    
Run Code Online (Sandbox Code Playgroud)

更新:

我把它通过周围的所有相关应用的见解API的方法调用,如工作trackException()this.appInsights.loadAppInsights()一个if statememt内:

import { ApplicationInsights } from "@microsoft/applicationinsights-web";

class MonitoringService {
  private appInsights: ApplicationInsights;
  constructor(@Inject(PLATFORM_ID) private platformId) {
    this.appInsights = new ApplicationInsights({
      config: {
        instrumentationKey: environment.appInsights.instrumentationKey,
      },
    });
    this.appInsights.loadAppInsights();
    // from documentation: https://docs.microsoft.com/en-us/azure/azure-    monitor/app/javascript#npm-based-setup:
    this.appInsights.trackPageView();
   }
    logException(exception: Error, severityLevel?: number) {
    if (isPlatformBrowser(this.platformId)) {
      this.appInsights.trackException({
        exception: exception,
        severityLevel: severityLevel,
      });
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

使用此解决方法,我猜expressApplication Insights 无法捕获服务器(Angular Universal Server,在我的情况下)上抛出的任何异常。因为全局错误处理程序,只有在平台是浏览器时才调用ai API。如果在浏览器之外运行的某些代码抛出异常会发生什么?有没有什么方法可以捕获这样的异常并写入ai,稍后我们回到浏览器平台时?