Cake logging with colours

123 Views Asked by At

I've read about cake's console aliases.

Is it possible to log using colours?

For example, I'd log success / warning / error in green / orange / red:

CustomWarning("WARN: Foo bar baz!");    // orange text
2

There are 2 best solutions below

0
Pascal Berger On BEST ANSWER

Cake comes with Spectre.Console as documented here: https://cakebuild.net/docs/writing-builds/console-output#advanced-output.

You can use it to print out formatted text:

AnsiConsole.Write(new Markup("[bold yellow]Hello[/] [red]World![/]"));

To have all logs, including the ones from Cake itself or addins, replaced, you can write a custom module with an ICakeLog implementation which uses Spectre.Console to write log messages.

See https://github.com/cake-contrib/Cake.BuildSystems.Module for an example of an module which implements custom logging.

0
lonix On

Use "Spectre.Console" instead:

#r "Spectre.Console"
using Spectre.Console
// ...
AnsiConsole.MarkupLine("[bold orangered1]WARN:[/] Foo bar baz!");