serilog / serilog-expressions

An embeddable mini-language for filtering, enriching, and formatting Serilog events, ideal for use with JSON or XML configuration.
Apache License 2.0
193 stars 17 forks source link

Add a `Sixteen` theme port #112

Closed kikaragyozov closed 8 hours ago

kikaragyozov commented 9 months ago

When I write something like the following:

loggerConfiguration.WriteTo.Console(new ExpressionTemplate(
    "[{@t:HH:mm:ss} {@l:u3}] {@m}\n{@x}", theme: TemplateTheme.Grayscale, applyThemeWhenOutputIsRedirected: true));

using .NET Aspire - I end up getting mumbo-jumbo in the Project log screen. I can imagine why, the question is - is there a way to make it support colors? If so, how?

The default console provider from Microsoft has colors. So I'm guessing something somewhere needs to be transcoded "the right way" in order for this to work as expected?

nblumhardt commented 9 months ago

Hi Kiril; my best bet here is that Aspire might support 16-color console output while the default themes here use 256-color ANSI codes, IIRC.

There's a theme called Sixteen that uses the simpler color codes. Does that one work for you?

kikaragyozov commented 9 months ago

Hi Kiril; my best bet here is that Aspire might support 16-color console output while the default themes here use 256-color ANSI codes, IIRC.

There's a theme called Sixteen that uses the simpler color codes. Does that one work for you?

I'm applying the theme by using an ExpressionTemplate with a TemplateTheme object. I can't find a TemplateTheme called Sixteen. Could you please show me how I'd be able to create a Console sink with the template you mentioned?

EDIT: You can see the exact code I used in my initial post.

kikaragyozov commented 9 months ago

I couldn't figure out how to do it with an ExpressionTemplate, so by just running loggerConfiguration.WriteTo.Console(theme: AnsiConsoleTheme.Sixteen, applyThemeToRedirectedOutput: true); everything worked as you'd expect. I have colors now. Thank you.

Do you reckon it's worth mentioning this to the Aspire folks for possible extended color support?

nblumhardt commented 9 months ago

I might move this over to the Serilog.Expressions repo and use it to track the addition of a Sixteen theme there - thanks for looping back!

I'm not sure what the plans/goals are for terminal output in Aspire, can't hurt to mention it to them, though 👍

tushroy commented 6 months ago

@nblumhardt On Windows Server 2012 R2, I found even using Sixteen equivalent theme doesn't produce colors when using ExpressionTemplate instead it produces garbage characters within template formatted message.

new ExpressionTemplate(expressionTemplate, theme: new TemplateTheme(
                new Dictionary<TemplateThemeStyle, string>
                {
                    [TemplateThemeStyle.Text] = "",
                    [TemplateThemeStyle.SecondaryText] = "",
                    [TemplateThemeStyle.TertiaryText] = "",
                    [TemplateThemeStyle.Invalid] = "\u001B[33m",
                    [TemplateThemeStyle.Null] = "\u001B[34m",
                    [TemplateThemeStyle.Name] = "",
                    [TemplateThemeStyle.String] = "\u001B[36m",
                    [TemplateThemeStyle.Number] = "\u001B[35m",
                    [TemplateThemeStyle.Boolean] = "\u001B[34m",
                    [TemplateThemeStyle.Scalar] = "\u001B[32m",
                    [TemplateThemeStyle.LevelVerbose] = "",
                    [TemplateThemeStyle.LevelDebug] = "\u001B[1m",
                    [TemplateThemeStyle.LevelInformation] = "\u001B[36;1m",
                    [TemplateThemeStyle.LevelWarning] = "\u001B[33;1m",
                    [TemplateThemeStyle.LevelError] = "\u001B[31;1m",
                    [TemplateThemeStyle.LevelFatal] = "\u001B[31;1m"
                }))

sample output: [←[0m09:23:57.442←[0m ←[0m←[36;1mINF←[0m (←[0m7b8e←[0m:←[0m31eb←[0m)←[0m] [←[0mRequestLoggingMiddleware←[0m] ←[0mIP ←[0m←[36m127.0.0.1←[0m client ←[0m←[36mMozilla/5.0 (Windows NT 6.3; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0←[0m requested ←[0m←[36mGET←[0m ←[0m←[36m/←[0m responded ←[0m←[35m200←[0m in ←[0m←[35m98.5781←[0m ms←[0m

Then I had to fallback to regular templating to get the colors back.

nblumhardt commented 6 months ago

@tushroy thanks for the follow-up. The old Windows console doesn't support ANSI colors at all :+1:

tushroy commented 6 months ago

@nblumhardt Thank you. That explain the problem. But is there any plan to support 16 Color with ExpressionTemplate. I want to use ExpressionTemplate because it provides more flexible templating system.

nblumhardt commented 6 months ago

Hi @tushroy - no immediate plan, but plugging in a sixteen color theme, as you are doing above, is supported.

nblumhardt commented 8 hours ago

Merged in https://github.com/serilog/serilog-expressions/pull/126