private void ConfigureCommandHandler()

in src/Commands/CommandFactory.cs [324:357]


    private void ConfigureCommandHandler(Command command, IBaseCommand implementation)
    {
        command.SetHandler(async context =>
        {
            _logger.LogTrace("Executing '{Command}'.", command.Name);

            var cmdContext = new CommandContext(_serviceProvider);
            var startTime = DateTime.UtcNow;
            try
            {
                var response = await implementation.ExecuteAsync(cmdContext, context.ParseResult);

                // Calculate execution time
                var endTime = DateTime.UtcNow;
                response.Duration = (long)(endTime - startTime).TotalMilliseconds;

                if (response.Status == 200 && response.Results == null)
                {
                    response.Results = ResponseResult.Create(new List<string>(), JsonSourceGenerationContext.Default.ListString);
                }

                Console.WriteLine(JsonSerializer.Serialize(response, _srcGenWithOptions.CommandResponse));
            }
            catch (Exception ex)
            {
                _logger.LogError("An exception occurred while executing '{Command}'. Exception: {Exception}",
                    command.Name, ex);
            }
            finally
            {
                _logger.LogTrace("Finished running '{Command}'.", command.Name);
            }
        });
    }