Skip to content

Add LoggerColorBehavior - #40221

Merged
maryamariyan merged 6 commits into
dotnet:masterfrom
maryamariyan:loggerbehavior
Aug 5, 2020
Merged

maryamariyan merged 6 commits into
dotnet:masterfrom
maryamariyan:loggerbehavior

Conversation

@maryamariyan

Copy link
Copy Markdown
Contributor

@ghost

ghost commented Jul 31, 2020

Copy link
Copy Markdown

Tagging subscribers to this area: @maryamariyan
See info in area-owners.md if you want to be subscribed.

@Dotnet-GitSync-Bot

Copy link
Copy Markdown
Collaborator

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@eerhardt eerhardt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Just a few minor clean ups and I think this is ready to merge.

@tarekgh tarekgh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put a little suggestions, other than that LGTM.

@maryamariyan
maryamariyan merged commit 646cfa3 into dotnet:master Aug 5, 2020
Jacksondr5 pushed a commit to Jacksondr5/runtime that referenced this pull request Aug 10, 2020
@karelz karelz added this to the 5.0.0 milestone Aug 18, 2020
@maryamariyan

maryamariyan commented Nov 5, 2020 •

Copy link
Copy Markdown
Contributor Author

Note on LoggerColorBehavior usage:

The intention behind LoggerColorBehavior is to provide formatters with a way to configure when ANSI color encoding is allowed to get padded around logged messages.

Therefore, if the log message itself contains some ANSI color codes, that gets logged as-is and LoggerColorBehavior is NOT designed/meant to remove that.

Sample usage:

The gist sample here implements a custom formatter that does color formatting conditioned by LoggerColorBehavior. Compare that against the built-in implementation of SimpleConsoleFormatter, which skips color coding for log level here:

bool disableColors = (FormatterOptions.ColorBehavior == LoggerColorBehavior.Disabled) ||
(FormatterOptions.ColorBehavior == LoggerColorBehavior.Default && System.Console.IsOutputRedirected);
if (disableColors)
{
return new ConsoleColors(null, null);
}

Hence, color coding logic within either formatters may be disabled using LoggerColorBehavior, but a call like:

logger.LogInformation("Random log \x1B[42mwith green background\x1B[49m message");

using either loggers will not strip out the padded color codes "\x1B[42m" and "\x1B[49m" within the message itself.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ConsoleLogger output is garbled with color codes when redirected

5 participants