我正在研究React-Native应用程序,并希望使用Application Insights来记录数据。为此,我静态地包含了AI JS SDK脚本。日志记录按预期工作,我可以记录跟踪和异常。但是,我要求每个日志(跟踪,异常或事件)都需要添加一些特定的自定义属性。
为了达到这个目的,我试图添加一个自定义遥测初始化器(我跟着这个link)。问题是我收到一条错误,指出AppInsights.queue.push
未定义。这是我的完整代码:
import Microsoft from "./ai.0";
type Props = {};
export default class App extends Component<Props> {
AppInsights = null;
constructor(props) {
super(props);
var snippet = {
config: {
instrumentationKey: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
}
};
var init = new Microsoft.ApplicationInsights.Initialization(snippet);
this.AppInsights = init.loadAppInsights();
this.AppInsights.trackPageView("First Page", null, {Comp: "App"},{hit:"1"}, 100); //This step is working
this.AppInsights.queue.push(function() { //This is where it breaks;
this.AppInsights.context.addTelemetryInitializer(function(envelope) {
var telemetryItem = envelope.data.baseData;
telemetryItem.Properties = telemetryItem.Properties || {};
telemetryItem.Properties["MyCustomProperty_1"] = "This is a custom property";
telemetryItem.Properties["MyCustomProperty_2"] = "This is another custom property";
});
});
}
我在这里缺少什么?
答案 0 :(得分:1)
这有点旧了,但是不需要直接访问队列或上下文。您可以直接从addTelemetryInitializer
实例中调用AppInsightsInitializer
:
init.addTelemetryInitializer((envelope) => {
var telemetryItem = envelope.data.baseData;
telemetryItem.Properties = telemetryItem.Properties || {};
telemetryItem.Properties["MyCustomProperty_1"] = "This is a custom property";
telemetryItem.Properties["MyCustomProperty_2"] = "This is another custom property";
});
还要确保在初始化AppInsights之后(通过调用loadAppInsights
)添加过滤器。
答案 1 :(得分:0)
我遇到了同样的问题以及在两次调用代码片段时发生的事实,并且在第二次没有&#34;队列&#34;了。解决方案是使用支票
包装代码段if (this.AppInsights.queue) {
this.AppInsights.queue.push(function() { //This is where it breaks;
this.AppInsights.context.addTelemetryInitializer(function(envelope) {
var telemetryItem = envelope.data.baseData;
telemetryItem.Properties = telemetryItem.Properties || {};
telemetryItem.Properties["MyCustomProperty_1"] = "This is a custom property";
telemetryItem.Properties["MyCustomProperty_2"] = "This is another custom property";
});
});
}