public static void Log(
            CommandContext context,
            Action <string> writer = null,
            bool includeSystemInfo = true,
            bool includeAppConfig  = false)
        {
            var config = context.AppConfig.Services.Get <CommandLoggerConfig>();

            if (config == null)
            {
                throw new AppRunnerException($"{nameof(CommandLoggerMiddleware)} has not been registered. " +
                                             $"Try `appRunner.{nameof(AppRunnerConfigExtensions.UseCommandLogger)}()`");
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.Services.AddOrUpdate(new CommandLoggerHasLoggedMarker());

            var sb = new StringBuilder(Environment.NewLine);

            sb.AppendLine("***************************************");

            var originalArgs = RemovePasswords(context, context.Original.Args.ToCsv(" "));

            sb.AppendLine("Original input:");
            sb.AppendLine($"  {originalArgs}");
            sb.AppendLine();

            var indent = new Indent();

            ParseReporter.Report(context, s => sb.AppendLine(s), indent);

            var additionalHeaders  = config.AdditionalHeadersCallback?.Invoke(context);
            var otherConfigEntries = GetOtherConfigInfo(context, includeSystemInfo, additionalHeaders).ToList();

            if (!otherConfigEntries.IsNullOrEmpty())
            {
                sb.AppendLine();
                var maxName = otherConfigEntries.Max(e => e.name.Length);
                foreach (var entry in otherConfigEntries)
                {
                    sb.AppendFormat($"{{0, -{maxName + 1}}} = {{1}}", entry.name, entry.text);
                    sb.AppendLine();
                }
            }

            if (includeAppConfig)
            {
                sb.AppendLine();
                sb.AppendLine(context.AppConfig.ToString(indent.Increment()));
            }

            sb.Append("***************************************");
            writer = writer ?? context.Console.Out.Write;
            writer(sb.ToString());
        }
Beispiel #2
0
        private static Task <int> ParseReportByArg(CommandContext commandContext, ExecutionDelegate next)
        {
            var parseContext = commandContext.Services.Get <ParseContext>();

            if (parseContext != null)
            {
                ParseReporter.Report(
                    commandContext,
                    commandContext.Console.Out.WriteLine);
                parseContext.Reported = true;
                return(Task.FromResult(0));
            }

            return(next(commandContext));
        }
Beispiel #3
0
        private static Task <int> ParseReportByArg(CommandContext commandContext, ExecutionDelegate next)
        {
            var parseContext = commandContext.Services.GetOrDefault <ParseContext>();

            if (parseContext != null)
            {
                ParseReporter.Report(
                    commandContext,
                    includeRawCommandLine: parseContext.IncludeRawCommandLine,
                    writeln: s => commandContext.Console.Out.WriteLine(s));
                parseContext.Reported = true;
                return(ExitCodes.Success);
            }

            return(next(commandContext));
        }
Beispiel #4
0
        internal static AppRunnerResult LogResult(this AppRunnerResult result, Action <string?> logLine, bool onError = false)
        {
            var print = onError || result.EscapedException != null
                ? result.Config.OnError.Print
                : result.Config.OnSuccess.Print;

            if (print.ConsoleOutput)
            {
                var consoleAll = result.Console.AllText();
                logLine($"{NewLine}Console output <begin> ------------------------------");
                logLine(consoleAll.IsNullOrWhitespace() ? "<no output>" : consoleAll);
                logLine($"Console output <end> ------------------------------{NewLine}");
            }

            var context = result.CommandContext;

            if (print.CommandContext)
            {
                logLine("");
                logLine(context?.ToString(new Indent(), includeOriginalArgs: true));
            }

            if (print.ParseReport && context?.ParseResult != null)
            {
                logLine("");
                logLine($"{NewLine}Parse report <begin> ------------------------------");
                ParseReporter.Report(context, writeln: logLine);
                logLine($"Parse report <end> ------------------------------{NewLine}");
            }

            if (print.AppConfig)
            {
                logLine("");
                logLine(result.Runner.ToString());
            }

            return(result);
        }