我正在Angular App中使用Azure Application Insights来记录客户端交互。
https://github.com/TrilonIO/angular-application-insights
我正在尝试阻止某个 URL 404 记录到应用程序见解。
有没有办法挂钩到发送到 App Insights 的数据,检查有问题的 URL 并阻止发送?
以下是我在应用程序组件中用于初始化的代码。
appInsightsService.config = {
instrumentationKey: 'appkey'
}
appInsightsService.init();
// associate user
appInsightsService.setAuthenticatedUserContext(user.name, user.account, true);
在 Angular 代码中,我没有显式调用任何 App Insight 日志记录方法。
我假设,您必须创建一个如下所示的应用程序洞察服务:
import {Injectable} from '@angular/core';
import {AppInsights} from 'applicationinsights-js';
@Injectable()
export class MonitoringService {
private config: Microsoft.ApplicationInsights.IConfig = {
instrumentationKey: 'KEY_GOES_HERE',
enableDebug: true,
verboseLogging: true
};
constructor() {
if (!AppInsights.config) {
AppInsights.downloadAndSetup(this.config);
}
}
logPageView(name?: string, url?: string, properties?: any, measurements?: any, duration?: number) {
AppInsights.trackPageView(name, url, properties, measurements, duration);
}
logEvent(name: string, properties?: any, measurements?: any) {
AppInsights.trackEvent(name, properties, measurements);
}
trackException(exception: Error) {
AppInsights.trackException(exception);
}
}
Angular 处理所有未捕获的异常,因此它也必须存储所有 404。尝试实现一个 https://angular.io/api/core/ErrorHandler 并从那里调用 trackException。
这将是一个自定义错误处理程序,您可以从中过滤要存储的 URL,也可以检查 404 的响应代码。如果为 true,则无需保存它,只需简单地调用跟踪异常方法即可。
这是一篇在角度 SPA 中配置 AI 的好文章。
http://www.andrewconnell.com/blog/using-azure-application-insights-with-single-page-apps
希望对您有所帮助。
我们的angular-application-insights
包实际上会自动将所有 URL 记录到 Azure 应用程序见解。
您可以通过传递来禁用此功能 overrideTrackPageMetrics: true
配置中
appInsightsService.config = {
overrideTrackPageMetrics: true, // <--
// other things
}
您可以从包本身放置类似于我在这里拥有的代码,然后自己处理路由日志记录!
我希望这对:)有所帮助