我在Angular App中使用Azure Application Insights来记录客户端交互。
https://github.com/TrilonIO/angular-application-insights
我正在尝试阻止某个URL 404被记录到Application Insights。
有没有办法挂钩发送到App Insights的数据,检查有问题的Url,并阻止发送?
下面是我在App Component中用来初始化的代码。
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的响应代码。如果是,则无需保存,只需调用跟踪异常方法即可。
这是一篇用于在角度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
}
您可以将类似代码放置到我拥有的here from the package itself,然后自己处理路由日志!
我希望有帮助:)