在 Application Insights 中,如何避免随每个请求发送会话自定义数据?

In Application Insights, how can I avoid sending session custom data with every request?

我目前正在 WPF 应用程序中使用 Application Insights。我几乎手动处理所有事情,因此在启动时我创建了一个遥测客户端并设置了多个上下文属性,如数据库版本、可用 RAM、监视器数量等。这是我们用来分段和过滤遥测数据的信息。

我遇到的问题是每次调用遥测客户端(TrackPageView、TrackEvent 等)时都会发送所有这些信息。有没有什么方法可以在每个会话中只发送一次信息,但仍然可以在门户中过滤和分段?

这是我的代码的摘录:

private static TelemetryClient _telemetry;

public static void Initialize(DeviceInfo deviceInfo, ApplicationInfo appInfo) {

    _telemetry = new TelemetryClient(TelemetryConfiguration.Active);
    _telemetry.Context.Device.OperatingSystem = deviceInfo.OsVersion;
    _telemetry.Context.Device.Id = deviceInfo.ProcessorId;
    _telemetry.Context.Component.Version = appInfo.Version;
    _telemetry.Context.Session.Id = Guid.NewGuid().ToString();
    _telemetry.Context.User.Id = appInfo.UserId;

    _telemetry.Context.Properties["Cpu"] = deviceInfo.Cpu;
    _telemetry.Context.Properties["NumberOfMonitors"] = deviceInfo.NumberOfMonitors;
    _telemetry.Context.Properties["TotalAvailableMemory"] = deviceInfo.TotalAvailableMemory;

}

public static void TrackPageView(string pageName) {
    _telemetry.TrackPageView(pageName);
}

不,没有 "built in" 方法可以在每个会话中仅发送一次该数据。

如果有,则必须有 sessions table,并且您必须在 sessionscustomEvents/[ 之间加入=13=] 在每个查询中获取该信息。然后必须完成一些技巧才能让您在会话进行时更新内容(如果有人在应用程序打开时插入监视器会怎样?)

作为替代方案,您可以创建一个事件,只发送一次您自己拥有该信息的事件。

但如果这样做,您将不得不再次开始加入该特定事件,并且您将无法轻松提出 "how often do people do X with 2 monitors" 之类的问题,您现在可以轻而易举地提出这些问题.