From c8590354a7c2d1c7ad2f1d3312edc141ef26c275 Mon Sep 17 00:00:00 2001 From: William Li Date: Wed, 26 Jul 2017 16:03:59 -0700 Subject: [PATCH] Add block thread constructor --- src/dotnet/Telemetry.cs | 26 +++++++++++++++++-- .../InternalReportinstallsuccessCommand.cs | 2 +- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/dotnet/Telemetry.cs b/src/dotnet/Telemetry.cs index 457e6f7af..cf5437a19 100644 --- a/src/dotnet/Telemetry.cs +++ b/src/dotnet/Telemetry.cs @@ -53,6 +53,28 @@ namespace Microsoft.DotNet.Cli _trackEventTask = Task.Factory.StartNew(() => InitializeTelemetry()); } + public Telemetry(IFirstTimeUseNoticeSentinel sentinel, string sessionId, bool blockThreadInitialization) + { + Enabled = !Env.GetEnvironmentVariableAsBool(TelemetryOptout) && PermissionExists(sentinel); + + if (!Enabled) + { + return; + } + + // Store the session ID in a static field so that it can be reused + CurrentSessionId = sessionId ?? Guid.NewGuid().ToString(); + + if (blockThreadInitialization) + { + InitializeTelemetry(); + } + else + { + _trackEventTask = Task.Factory.StartNew(() => InitializeTelemetry()); + } + } + private bool PermissionExists(IFirstTimeUseNoticeSentinel sentinel) { if (sentinel == null) @@ -126,9 +148,9 @@ namespace Microsoft.DotNet.Cli _client.TrackEvent(eventName, eventProperties, eventMeasurements); _client.Flush(); } - catch (Exception) + catch (Exception e) { - Debug.Fail("Exception during TrackEventTask"); + Debug.Fail(e.ToString()); } } diff --git a/src/dotnet/commands/dotnet-internal-reportinstallsuccess/InternalReportinstallsuccessCommand.cs b/src/dotnet/commands/dotnet-internal-reportinstallsuccess/InternalReportinstallsuccessCommand.cs index 14f940c07..9da815196 100644 --- a/src/dotnet/commands/dotnet-internal-reportinstallsuccess/InternalReportinstallsuccessCommand.cs +++ b/src/dotnet/commands/dotnet-internal-reportinstallsuccess/InternalReportinstallsuccessCommand.cs @@ -43,7 +43,7 @@ namespace Microsoft.DotNet.Cli { var sessionId = Environment.GetEnvironmentVariable(TelemetrySessionIdEnvironmentVariableName); - telemetry = new Telemetry(new NoOpFirstTimeUseNoticeSentinel(), sessionId); + telemetry = new Telemetry(new NoOpFirstTimeUseNoticeSentinel(), sessionId, blockThreadInitialization: true); } public bool Enabled => telemetry.Enabled;