diff --git a/src/Microsoft.DotNet.Cli.Utils/CommandContext.cs b/src/Microsoft.DotNet.Cli.Utils/CommandContext.cs index 1776d3ebf..8209dd2f7 100644 --- a/src/Microsoft.DotNet.Cli.Utils/CommandContext.cs +++ b/src/Microsoft.DotNet.Cli.Utils/CommandContext.cs @@ -14,8 +14,8 @@ namespace Microsoft.DotNet.Cli.Utils public static readonly string AnsiPassThru = Prefix + "ANSI_PASS_THRU"; } - private static Lazy _verbose = new Lazy(() => Env.GetBool(Variables.Verbose)); - private static Lazy _ansiPassThru = new Lazy(() => Env.GetBool(Variables.AnsiPassThru)); + private static Lazy _verbose = new Lazy(() => Env.GetEnvironmentVariableAsBool(Variables.Verbose)); + private static Lazy _ansiPassThru = new Lazy(() => Env.GetEnvironmentVariableAsBool(Variables.AnsiPassThru)); public static bool IsVerbose() { diff --git a/src/Microsoft.DotNet.Cli.Utils/Env.cs b/src/Microsoft.DotNet.Cli.Utils/Env.cs index 96cfd2d1e..5d0d1382b 100644 --- a/src/Microsoft.DotNet.Cli.Utils/Env.cs +++ b/src/Microsoft.DotNet.Cli.Utils/Env.cs @@ -34,28 +34,9 @@ namespace Microsoft.DotNet.Cli.Utils return _environment.GetCommandPathFromRootPath(rootPath, commandName, extensions); } - public static bool GetBool(string name, bool defaultValue = false) + public static bool GetEnvironmentVariableAsBool(string name, bool defaultValue = false) { - var str = Environment.GetEnvironmentVariable(name); - if (string.IsNullOrEmpty(str)) - { - return defaultValue; - } - - switch (str.ToLowerInvariant()) - { - case "true": - case "1": - case "yes": - return true; - case "false": - case "0": - case "no": - return false; - default: - return defaultValue; - } + return _environment.GetEnvironmentVariableAsBool(name, defaultValue); } - } } diff --git a/src/Microsoft.DotNet.Cli.Utils/EnvironmentProvider.cs b/src/Microsoft.DotNet.Cli.Utils/EnvironmentProvider.cs index 1060443ff..b44ffef71 100644 --- a/src/Microsoft.DotNet.Cli.Utils/EnvironmentProvider.cs +++ b/src/Microsoft.DotNet.Cli.Utils/EnvironmentProvider.cs @@ -93,5 +93,29 @@ namespace Microsoft.DotNet.Cli.Utils return GetCommandPathFromRootPath(rootPath, commandName, extensionsArr); } + + public bool GetEnvironmentVariableAsBool(string name, bool defaultValue) + { + var str = Environment.GetEnvironmentVariable(name); + if (string.IsNullOrEmpty(str)) + { + return defaultValue; + } + + switch (str.ToLowerInvariant()) + { + case "true": + case "1": + case "yes": + return true; + case "false": + case "0": + case "no": + return false; + default: + return defaultValue; + } + } + } } diff --git a/src/Microsoft.DotNet.Cli.Utils/IEnvironmentProvider.cs b/src/Microsoft.DotNet.Cli.Utils/IEnvironmentProvider.cs index db04ebc0b..754bf54a3 100644 --- a/src/Microsoft.DotNet.Cli.Utils/IEnvironmentProvider.cs +++ b/src/Microsoft.DotNet.Cli.Utils/IEnvironmentProvider.cs @@ -16,5 +16,7 @@ namespace Microsoft.DotNet.Cli.Utils string GetCommandPathFromRootPath(string rootPath, string commandName, params string[] extensions); string GetCommandPathFromRootPath(string rootPath, string commandName, IEnumerable extensions); + + bool GetEnvironmentVariableAsBool(string name, bool defaultValue); } } diff --git a/src/Microsoft.DotNet.Cli.Utils/Telemetry.cs b/src/Microsoft.DotNet.Cli.Utils/Telemetry.cs index 2a2bab2d1..c3ca6eddc 100644 --- a/src/Microsoft.DotNet.Cli.Utils/Telemetry.cs +++ b/src/Microsoft.DotNet.Cli.Utils/Telemetry.cs @@ -7,51 +7,44 @@ namespace Microsoft.DotNet.Cli.Utils { public class Telemetry { - private class Variables - { - public static readonly string InstrumentationKey = "74cc1c9e-3e6e-4d05-b3fc-dde9101d0254"; - private static readonly string Prefix = "DOTNET_CLI_TELEMETRY_"; - public static readonly string Optout = Prefix + "OPTOUT"; - } - - private class Properties - { - public static readonly string OSVersion = "OS Version"; - public static readonly string OSPlatform = "OS Platform"; - public static readonly string RuntimeId = "Runtime Id"; - public static readonly string ProductVersion = "Product Version"; - } - private static bool _isInitialized = false; private static TelemetryClient _client = null; private static Dictionary _commonProperties = null; private static Dictionary _commonMeasurements = null; - static Telemetry() + private static readonly string InstrumentationKey = "74cc1c9e-3e6e-4d05-b3fc-dde9101d0254"; + + private const string TelemetryOptout = "DOTNET_CLI_TELEMETRY_OPTOUT"; + private const string OSVersion = "OS Version"; + private const string OSPlatform = "OS Platform"; + private const string RuntimeId = "Runtime Id"; + private const string ProductVersion = "Product Version"; + + public Telemetry() { if (_isInitialized) return; - bool Optout = Env.GetBool(Variables.Optout); + bool optout = Env.GetEnvironmentVariableAsBool(TelemetryOptout); - if (Optout) + if (optout) return; try { _client = new TelemetryClient(); - _client.InstrumentationKey = Variables.InstrumentationKey; + _client.InstrumentationKey = InstrumentationKey; _client.Context.Session.Id = Guid.NewGuid().ToString(); var runtimeEnvironment = PlatformServices.Default.Runtime; _client.Context.Device.OperatingSystem = runtimeEnvironment.OperatingSystem; _commonProperties = new Dictionary(); - _commonProperties.Add(Properties.OSVersion, runtimeEnvironment.OperatingSystemVersion); - _commonProperties.Add(Properties.OSPlatform, runtimeEnvironment.OperatingSystemPlatform.ToString()); - _commonProperties.Add(Properties.RuntimeId, runtimeEnvironment.GetRuntimeIdentifier()); - _commonProperties.Add(Properties.ProductVersion, Product.Version); + _commonProperties.Add(OSVersion, runtimeEnvironment.OperatingSystemVersion); + _commonProperties.Add(OSPlatform, runtimeEnvironment.OperatingSystemPlatform.ToString()); + _commonProperties.Add(RuntimeId, runtimeEnvironment.GetRuntimeIdentifier()); + _commonProperties.Add(ProductVersion, Product.Version); _commonMeasurements = new Dictionary(); _isInitialized = true; @@ -59,23 +52,24 @@ namespace Microsoft.DotNet.Cli.Utils catch (Exception) { } } - public static void TrackCommand(string command, IDictionary properties = null, IDictionary measurements = null) + public void TrackCommand(string command, IDictionary properties = null, IDictionary measurements = null) { if (!_isInitialized) return; - Dictionary eventProperties = new Dictionary(_commonProperties); - if (properties != null) - { - foreach (var p in properties) - { - if (eventProperties.ContainsKey(p.Key)) - eventProperties[p.Key] = p.Value; - else - eventProperties.Add(p.Key, p.Value); - } - } + Dictionary eventMeasurements = GetEventMeasures(measurements); + Dictionary eventProperties = GetEventProperties(properties); + try + { + _client.TrackEvent(command, eventProperties, eventMeasurements); + _client.Flush(); + } + catch (Exception) { } + } + + private Dictionary GetEventMeasures(IDictionary measurements) + { Dictionary eventMeasurements = new Dictionary(_commonMeasurements); if (measurements != null) { @@ -87,13 +81,23 @@ namespace Microsoft.DotNet.Cli.Utils eventMeasurements.Add(m.Key, m.Value); } } + return eventMeasurements; + } - try + private Dictionary GetEventProperties(IDictionary properties) + { + Dictionary eventProperties = new Dictionary(_commonProperties); + if (properties != null) { - _client.TrackEvent(command, eventProperties, eventMeasurements); - _client.Flush(); + foreach (var p in properties) + { + if (eventProperties.ContainsKey(p.Key)) + eventProperties[p.Key] = p.Value; + else + eventProperties.Add(p.Key, p.Value); + } } - catch (Exception) { } + return eventProperties; } } } diff --git a/src/dotnet/Program.cs b/src/dotnet/Program.cs index 6659f000f..f9a6ec42d 100644 --- a/src/dotnet/Program.cs +++ b/src/dotnet/Program.cs @@ -117,18 +117,13 @@ namespace Microsoft.DotNet.Cli ["test"] = TestCommand.Run }; - int exitCode = 100; - - string arguments = string.Empty; - - + int exitCode; + var arguments = string.Empty; Func builtIn; if (builtIns.TryGetValue(command, out builtIn)) { exitCode = builtIn(appArgs.ToArray()); - - appArgs.ToList().ForEach(a => { arguments += a + " "; }); - + arguments = string.Join(" ", appArgs); } else { @@ -140,7 +135,9 @@ namespace Microsoft.DotNet.Cli exitCode = result.ExitCode; } - Telemetry.TrackCommand( + Telemetry telemetryClient = new Telemetry(); + + telemetryClient.TrackCommand( command, new Dictionary { @@ -155,7 +152,7 @@ namespace Microsoft.DotNet.Cli } -private static void PrintVersion() + private static void PrintVersion() { Reporter.Output.WriteLine(Product.Version); }