Closed borland closed 8 months ago
Thanks for the suggestion!
Should NO_COLOR
apply even if a theme is set explicitly?
I would suggest that it should override even an explicitly set theme. A setting in code tends to be product/team wide whereas an environment variable tends to be scoped to the machine/container. More specific wins over less specific, if that makes sense. Thanks!
Sounds good to me, should be fairly straightforward to implement directly in the WriteTo.Console
method, by switching the theme when the variable is detected.
Anyone following along keen to take a shot at a PR?
If one is unable to set the NO_COLOR
env var, then there's another way:
.WriteTo.Console(
#if DEBUG
theme: AnsiConsoleTheme.Code
#else
theme: AnsiConsoleTheme.None
#endif
)
For JSON config, one could use an appsettings.json
and appsettings.Development.json
pair.
We use Serilog directly in a number of systems, and indirectly via some Nuke build scripts -- Nuke uses Serilog itself.
We enjoy colored console logging when running interactively, but this same code is often run in Automated Builds (TeamCity, GitHub actions, etc) where stdout is captured by the build system - sometimes indirectly through
docker run
ordocker compose
.This leads to messy log files full of escape sequences such as this:
I am aware that this package supports
ConsoleTheme.None
to disable colorization, but we do not want to alter our code (or in the case of Nuke, cannot directly alter it), as then we would lose the nice coloring when running interactively on developer systems.There are also important accessibility and compatibility reasons that people want to disable color. Colorblindness, or colors that conflict with terminal themes, etc.
The
NO_COLOR=1
environment variable has emerged as a de-facto standard for this, and is supported by a wide range of software. More information at https://no-color.org/Build and run this small C# sample application.
When executed with the NO_COLOR env var set to 1, I expect to see no colors printed. But: I see colorized output
Thanks!