using System.CommandLine; using System.CommandLine.Invocation; using System.CommandLine.IO; using System.CommandLine.Rendering; namespace CustomHostingDemo.Logging { public class CliLogger : ICliLogger { private readonly IConsole _console; private readonly IDefaultArgs _args; public CliLogger(InvocationContext context, IDefaultArgs args) { _console = context.Console; _args = args; } public ICliLogger LogContent(string message) { return Log(new ContentSpan(message)); } public ICliLogger LogError(string message) { return Log(new ContainerSpan( ForegroundColorSpan.Red(), new ContentSpan("[ERROR] "), new ContentSpan(message), ForegroundColorSpan.Reset() )); } public ICliLogger LogInfo(string message) { if (!_args.Verbose) { return this; } return Log(new ContainerSpan( ForegroundColorSpan.LightGray(), new ContentSpan("[INFO] "), new ContentSpan(message), ForegroundColorSpan.Reset() )); } public ICliLogger LogWarning(string message) { return Log(new ContainerSpan( ForegroundColorSpan.Yellow(), new ContentSpan("[WARN] "), new ContentSpan(message), ForegroundColorSpan.Reset() )); } private ICliLogger Log(TextSpan span) { _console.Out.WriteLine(span.ToString(OutputMode.Ansi)); return this; } } }