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

Grand Unified Console [WIP] #8

Merged
merged 16 commits into from
Jun 19, 2017

Conversation

nblumhardt
Copy link
Member

Started work on #7 - just a very basic merge of Serilog.Sinks.Console and Serilog.Sinks.LiterateConsole at this point.

@nblumhardt nblumhardt changed the title Great console unification [WIP] Grand Unified Console [WIP] Jun 18, 2017
@nblumhardt
Copy link
Member Author

nblumhardt commented Jun 18, 2017

10 hours of laptop battery and a flight from Oslo to Brisbane later...

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .WriteTo.Console()
    .CreateLogger();

image

As you can see, the default console output now uses the "literate" theme, and has an updated outputTemplate including unquoted/JSON message template rendering.

Here's what the template looks like behind the scenes:

"[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"

Original style

The following configuration produces the same output as the original Serilog.Sinks.Console:

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .WriteTo.Console(
        outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message}{NewLine}{Exception}",
        theme: ConsoleTheme.None)
    .CreateLogger();

Notice the omission of :lj from {Message}, and the theme parameter. F5:

image

ANSI output

ANSI themes are supported:

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .WriteTo.Console(theme: AnsiConsoleTheme.Literate)
    .CreateLogger();

On Win 10, these seem to work from the PowerShell console only, I can't get Visual Studio's F5 experience to work with it (just shows garbage).

image

The 'literate' theme doesn't transfer to 16-color ANSI very well, but it's passable.

Other

Custom output via formatters such as CompactJsonFormatter is supported (not styled though, of course).

Themes can be created by constructing instances of the SystemConsoleTheme or AnsiConsoleTheme classes. The existing themes can be modified by creating a new style dictionary based on their .Styles properties.

The design of OutputTemplateRenderer is much better than the one I just committed to serilog/serilog, although this one allocates in a couple of places the other doesn't (most prominently in handling the {Properties} special token). The output template is inspected only once at construction, instead of during the rendering of each event. Hoping to take learnings from this exercise to improve the one over there in a future PR.

To do (maybe post-PR, if anyone's keen to get their hands on this and try building some themes, first ;-)):

  • No tests yet, unfortunately (huge churn while I figured a workable design out)
  • Monochrome/grayscale ANSI and System Console themes (AnsiConsoleTheme.Gray and SystemConsoleTheme.Gray)
  • A VS Code-style ANSI theme, maybe 256-color (AnsiConsoleTheme.Code)
  • A WebStorm "Darcula"-style ANSI theme (AnsiConsoleTheme.Storm)
  • See if it's possible to get ANSI output working with VS "F5" on Windows 10

@nblumhardt
Copy link
Member Author

Grayscale

image

@nblumhardt
Copy link
Member Author

Code

image

@nblumhardt
Copy link
Member Author

VS Code-style theme is in; going to call it a day for now. Will add a couple of tests to get the ball rolling, then call this "done".

@khellang
Copy link
Member

We should detect OS and version and P/Invoke Kernel32's SetConsoleMode to enable VT100 sequences on Windows 10. That should make conhost respect the CSI codes instead of writing garbage. I can PR this later if you want.

@merbla
Copy link
Contributor

merbla commented Jun 18, 2017

Should we provide a few sample projects, given the increased options available?

@nblumhardt
Copy link
Member Author

Thanks @khellang! I'd searched for something like it but had no luck. Implementing now...

@merbla definitely! I'm running low on time for this one though, perhaps it's one for the backlog?

@merbla
Copy link
Contributor

merbla commented Jun 18, 2017

.NET Command Line Tools (1.0.4)

Product Information:
 Version:            1.0.4
 Commit SHA-1 hash:  af1e6684fd

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.11
 OS Platform: Darwin
 RID:         osx.10.11-x64
 Base Path:   /usr/local/share/dotnet/sdk/1.0.4

OSX/ZSH

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .WriteTo.Console()
    .CreateLogger();

image

@nblumhardt
Copy link
Member Author

Woot! Thanks @merbla 👍

@nblumhardt
Copy link
Member Author

@merbla tried the AnsiConsoleTheme.Code variant?

@merbla
Copy link
Contributor

merbla commented Jun 18, 2017

.NET Command Line Tools (1.0.4)

Product Information:
 Version:            1.0.4
 Commit SHA-1 hash:  af1e6684fd

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.11
 OS Platform: Darwin
 RID:         osx.10.11-x64
 Base Path:   /usr/local/share/dotnet/sdk/1.0.4

OSX/ZSH

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .WriteTo.Console(theme: AnsiConsoleTheme.Literate)
    .CreateLogger();

image

@nblumhardt
Copy link
Member Author

👍 ... .Code, though? :-)

@merbla
Copy link
Contributor

merbla commented Jun 18, 2017

.NET Command Line Tools (1.0.4)

Product Information:
 Version:            1.0.4
 Commit SHA-1 hash:  af1e6684fd

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.11
 OS Platform: Darwin
 RID:         osx.10.11-x64
 Base Path:   /usr/local/share/dotnet/sdk/1.0.4

OSX/ZSH

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .WriteTo.Console(
        outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message}{NewLine}{Exception}",
        theme: ConsoleTheme.None)
    .CreateLogger();

image

@merbla
Copy link
Contributor

merbla commented Jun 18, 2017

.NET Command Line Tools (1.0.4)

Product Information:
 Version:            1.0.4
 Commit SHA-1 hash:  af1e6684fd

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.11
 OS Platform: Darwin
 RID:         osx.10.11-x64
 Base Path:   /usr/local/share/dotnet/sdk/1.0.4

OSX/ZSH

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .WriteTo.Console(theme: AnsiConsoleTheme.Code)
    .CreateLogger();

image

@nblumhardt
Copy link
Member Author

Success! Though now I am wondering if we can come up with a better theme than that..... :-)

@merbla
Copy link
Contributor

merbla commented Jun 18, 2017

Are we thinking this is a 3.0 release of console given the changes? 🙈


<PropertyGroup>
<Description>A Serilog sink that writes log events to the console/terminal.</Description>
<VersionPrefix>3.0.0</VersionPrefix>
Copy link
Member Author

Choose a reason for hiding this comment

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

@merbla yep :-)

Copy link
Contributor

Choose a reason for hiding this comment

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

soz missed that one. I was looking for project.json 😜

Copy link
Member Author

Choose a reason for hiding this comment

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

Still find myself doing that, too.

@nblumhardt
Copy link
Member Author

ANSI with Windows 10 cmd.exe (SetConsoleMode() did it, thanks Kristian.)

image

</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.5.0-dev-00842" />
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't forget to circle back to this, post 2.5

@merbla
Copy link
Contributor

merbla commented Jun 18, 2017

@nblumhardt when you are comfortable, lets get this in and built for dev.

@nblumhardt
Copy link
Member Author

nblumhardt commented Jun 18, 2017

Okay, close enough to "tested" - just exercises the renderers by switching off the theme and running them through the Serilog tests.

There are actually a couple of other potentially-breaking changes here; I think they're reasonably harmless, but ... full disclosure :-)

  • Rendered objects in display formatting now use C#-style entirely, e.g. {Foo="bar", ...} instead of the old { Foo: "bar", ... } - JSON formatting is the default however, so this makes no difference unless opting into the C#-like "display" syntax
  • Dictionaries in display formatting have been updated to use new C# syntax, {["Foo"]="bar", ...} instead of [("Foo", "bar"), ...]
  • Whitespace in display formatting around some punctuation has been trimmed, as above
  • Non-literal strings now always escape all non-printable characters, e.g. newlines (literal formatting is off by default now, so newlines are emitted directly)

To get 100% full fidelity with version 2 of this sink, you can use:

    .WriteTo.Console(new MessageTemplateTextFormatter(
        "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level}] {Message}{NewLine}{Exception}",
        null))

This will bypass the custom/themed formatters and use Serilog's built-in formatting, which is as-yet unchanged (I'd like to discuss taking the C#-style object and dictionary syntax back there, too, in another thread :-)).

One tiny dependency version change, then I'll hit the button! Thoughts on the above welcome.

@nblumhardt nblumhardt merged commit c961117 into serilog:dev Jun 19, 2017
@khellang
Copy link
Member

khellang commented Jun 19, 2017

Just a thought; what happens with ANSI escape codes when standard output is redirected? Should we also check Console.IsOutputRedirected before emitting them? The .NET Core PAL checks this before emitting escape codes when setting ForegroundColor and BackgroundColor.

@nblumhardt
Copy link
Member Author

@khellang
Copy link
Member

Oh, sorry, didn't notice it. Awesome! 😁

@nblumhardt nblumhardt mentioned this pull request Jun 21, 2017
@hellboy81
Copy link

hellboy81 commented Oct 19, 2018

Which Terminal settings should be applied to display AnsiConsoleTheme.Code output in VS Code itself properly?

At the moment control chars are not interpreted:

image

@khellang
Copy link
Member

The VS Code terminal should support ANSI escape codes out of the box. Is that output from a running app?

@hellboy81
Copy link

hellboy81 commented Oct 19, 2018

fixed removing using theme parameter.

Is that output from a running app?

yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants