Printing

Printing combines logging with Console.Write statements. All printed messages are sent to the logger - if a logger was configured. INFO, WARN and ERROR messages are sent to Console, too.

public Mult3Command(IServiceProvider serviceProvider) : base(serviceProvider) { }

protected override void Execute()
{
    Print.Debug($"Serialized Context='{JsonSerializer.Serialize(this.Context)}'");
    decimal result = this.Context.Factor1 * this.Context.Factor2;
    Print.Info($"{this.Context.Factor1} * {this.Context.Factor2} = {result}");
}

Using the Printer with the nlog configuration from Demo5 you will see the following output:

Demo05 - Logging and Printing
MSPro.CLArgs.ConsolePrinter: 5,43 * 7,54 = 40,9422
5,43 * 7,54 = 40,9422

The first message MSPro.CLArgs.ConsolePrinter... apears, because the logger is configured to log to the console. The second line appers because Print.Info prints to Console, too.

The advantage of using Print is, that you can use logging but you can also go without. If you decide to implement logging later you can do this without changing your codebase. In addition, Print is always there i(in the base class).

FInally, you can implement and provide your own IPrinter implementation to control the outout as you want it. See the ConsolePrinter for reference implementation.

Last updated