发布于 2020-11-16 00:28:29
发布于 2020-11-13 15:42:04
最后,我用来自AppInsights的这里创建了一个与所有事物相关的公共服务。
import { AngularPlugin, AngularPluginService } from '@microsoft/applicationinsights-angularplugin-js';
import {
ApplicationInsights,
IEventTelemetry,
IExceptionTelemetry
} from '@microsoft/applicationinsights-web';
....
export class ApplicationInsightService {
private appInsights: ApplicationInsights;
constructor(private router: Router, private angularPluginService: AngularPluginService) {
const angularPlugin = new AngularPlugin();
this.angularPluginService.init(angularPlugin, this.router);
this.appInsights = new ApplicationInsights({ config: {
instrumentationKey: 'Your key here',
extensions: [angularPlugin],
extensionConfig: {
[angularPlugin.identifier]: { router: this.router },
}
}});
this.appInsights.loadAppInsights();
this.appInsights.trackPageView();
}
setUserId(userId: string) {
this.appInsights.setAuthenticatedUserContext(userId);
}
clearUserId() {
this.appInsights.clearAuthenticatedUserContext();
}
logPageView(name?: string, uri?: string) {
this.appInsights.trackPageView({ name, uri });
}
logEvent(event: IEventTelemetry, customProperties: { [key: string]: any }) {
this.appInsights.trackEvent(event, customProperties);
}
trackError(exception: Error) {
let telemetry = {
exception,
} as IExceptionTelemetry;
this.appInsights.trackException(telemetry);
}
stopTelemetry() { // !! this is how you stop tracking
this.appInsights.config.disableTelemetry = true;
}
startTelemetry() { // !! this is how you start/re-start it
this.appInsights.config.disableTelemetry = false;
}
}
JavaScript部分对文档没有帮助,因为这些方法/属性并不存在,但是C#文档提供了帮助,而且它似乎与此类似。
https://stackoverflow.com/questions/64670400
复制相似问题