public static void InitializeTelemetry() { string dll = Assembly.GetEntryAssembly().GetName().Name; if (dll == "aggregator-cli") { telemetrySettings = CliTelemetrySettings.Get(); WaitAtShutdown = false; } else if (dll == "aggregator-host") { telemetrySettings = HostTelemetrySettings.Get(); WaitAtShutdown = true; } else { // another option would be to disable telemetry throw new InvalidOperationException("Telemetry can be used only in CLI or Host"); } TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault(); configuration.InstrumentationKey = ApplicationInsightsKey; // use default InMemory channel, if there are network issues, who cares configuration.TelemetryChannel.DeveloperMode = Debugger.IsAttached; #if DEBUG configuration.TelemetryChannel.DeveloperMode = true; #endif Enabled = telemetrySettings.Enabled; if (Enabled) { telemetryClient = new TelemetryClient(configuration); // this is portable telemetryClient.Context.User.Id = telemetrySettings.UserId; // this is time based, cannot be a new one at each run telemetryClient.Context.Session.Id = telemetrySettings.SessionId; telemetryClient.Context.Session.IsFirst = telemetrySettings.IsNewSession; ///Environment.Is64BitOperatingSystem; telemetryClient.Context.Device.OperatingSystem = Environment.OSVersion.ToString(); telemetryClient.Context.Device.Id = telemetrySettings.DeviceId; telemetryClient.Context.Component.Version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); Trace.WriteLine(string.Format("SessionID: {0}", telemetryClient.Context.Session.Id)); } }
public static ITelemetrySettings Get() { CliTelemetrySettings s; if (File.Exists(SettingsFileName)) { s = JsonConvert.DeserializeObject <CliTelemetrySettings>( File.ReadAllText(SettingsFileName)); bool sessionExpired = DateTime.Now.Subtract(MaxSessionDuration) > s.LastUpdate; if (sessionExpired) { s.SessionId = Guid.NewGuid().ToString(); s.IsNewSession = true; } else { s.IsNewSession = false; } } else { s = new CliTelemetrySettings() { SessionId = Guid.NewGuid().ToString(), IsNewSession = true, // I assume these won't change during a session :-) TrueUserId = Environment.UserName, TrueDeviceId = Environment.MachineName, UserId = GetHash($"{Environment.UserName}@{Environment.MachineName}"), DeviceId = GetHash(Environment.MachineName), }; } s.Enabled = !EnvironmentVariables.GetAsBool("AGGREGATOR_TELEMETRY_DISABLED", false); return(s); }