From 1cce6f252ae2e10c1c23ecd2ed422ab1a3b5a324 Mon Sep 17 00:00:00 2001 From: Alexandre Zollinger Chohfi Date: Fri, 31 Oct 2025 11:32:55 -0700 Subject: [PATCH] Improve telemetry parsing. --- .../Telemetry/Events/CommandInvokedEvent.cs | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/winapp-CLI/WinApp.Cli/Telemetry/Events/CommandInvokedEvent.cs b/src/winapp-CLI/WinApp.Cli/Telemetry/Events/CommandInvokedEvent.cs index f474864..4296431 100644 --- a/src/winapp-CLI/WinApp.Cli/Telemetry/Events/CommandInvokedEvent.cs +++ b/src/winapp-CLI/WinApp.Cli/Telemetry/Events/CommandInvokedEvent.cs @@ -4,6 +4,7 @@ using Microsoft.Diagnostics.Telemetry; using Microsoft.Diagnostics.Telemetry.Internal; using System.CommandLine; +using System.CommandLine.Help; using System.CommandLine.Parsing; using System.Diagnostics.Tracing; using System.Text.Json; @@ -25,32 +26,43 @@ internal class CommandInvokedEvent : EventBase internal CommandInvokedEvent(CommandResult commandResult, DateTime startedTime) { CommandName = commandResult.Command.GetType().FullName!; - var argumentsDict = commandResult.Children - .OfType() - .ToDictionary(a => a.Argument.Name, GetValue); - var optionsDict = commandResult.Children - .OfType() - .ToDictionary(o => o.Option.Name, GetValue); - var commandExecutionContext = new CommandExecutionContext(argumentsDict, optionsDict); - Context = JsonSerializer.Serialize(commandExecutionContext, CommandInvokedEventJsonContext.Default.CommandExecutionContext); + try + { + var argumentsDict = commandResult.Children + .OfType() + .ToDictionary(a => a.Argument.Name, GetValue); + var optionsDict = commandResult.Children + .OfType() + .ToDictionary(o => o.Option.Name, GetValue); + var commandExecutionContext = new CommandExecutionContext(argumentsDict, optionsDict); + Context = JsonSerializer.Serialize(commandExecutionContext, CommandInvokedEventJsonContext.Default.CommandExecutionContext); + } + catch (Exception ex) + { + Context = $"[error parsing context]: {ex.Message}"; + } StartedTime = startedTime; } private string? GetValue(OptionResult o) { - return !o.Errors.Any() ? GetValue(o.Option.ValueType, o.Implicit, o.GetValueOrDefault()) : "[error]"; + return o.Option is HelpOption + ? "true" + : !o.Errors.Any() ? GetValue(o.Option.ValueType, o.Implicit, () => o.GetValueOrDefault()) : "[error]"; } private string? GetValue(ArgumentResult a) { - return !a.Errors.Any() ? GetValue(a.Argument.ValueType, a.Implicit, a.GetValueOrDefault()) : "[error]"; + return !a.Errors.Any() + ? GetValue(a.Argument.ValueType, a.Implicit, () => a.GetValueOrDefault()) + : (a.Errors.Any(e => e.Message.StartsWith("Required argument missing for command:")) ? null : "[error]"); } - private static string? GetValue(Type valueType, bool isImplicit, object? value) + private static string? GetValue(Type valueType, bool isImplicit, Func value) { return isImplicit ? null : ((valueType == typeof(string) || valueType == typeof(FileInfo) || - valueType == typeof(DirectoryInfo)) ? "[string]" : value)?.ToString(); + valueType == typeof(DirectoryInfo)) ? "[string]" : value != null ? value() : null)?.ToString(); } public string CommandName { get; private set; }