Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LoggerColorBehavior #40221

Merged
merged 6 commits into from
Aug 5, 2020
Merged

Conversation

maryamariyan
Copy link
Member

@ghost
Copy link

ghost commented Jul 31, 2020

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

@Dotnet-GitSync-Bot
Copy link
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.

Copy link
Member

@eerhardt eerhardt left a comment

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.

Copy link
Member

@tarekgh tarekgh left a comment

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
Copy link
Member Author

maryamariyan commented Nov 5, 2020

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