Skip to content

Microsoft.Extensions.Logging: Console logger option to keep lines in message with SingleLine true #45402

Closed
@xoofx

Description

@xoofx

I have a problem with the ConsoleLogger that doesn't do what I'm actually looking for in terms of SingleLine experience.

Basically, I would like to have the log happening on a single line if the message doesn't have new lines, but otherwise it would keep the new lines around:

Today we have the following two modes:

  • builder.AddSimpleConsole();
info: lunet[0]
      Site build started
      With a long new lines
      But we want to preserve the messages lines
info: lunet[1]
      This is another simple line but the message doesn't have new lines
info: lunet[2]
      Site build finished in 1194.4774ms. 71 files processed. 2073934 bytes written.
  • .AddSimpleConsole(options => { options.SingleLine = true; });
info: lunet[0] Site build started With a long new lines But we want to preserve the messages lines
info: lunet[1] This is another simple line but the message doesn't have new lines
info: lunet[2] Site build finished in 1216.9315ms. 71 files processed. 2073934 bytes written.

While I would like to that this mode:

info: lunet[0] Site build started
      With a long new lines
      But we want to preserve the messages lines
info: lunet[1] This is another simple line but the message doesn't have new lines
info: lunet[2] Site build finished in 1211.6869ms. 71 files processed. 2073934 bytes written.

Note also that the code in SimpleConsoleFormatter.WriteReplacing is a bit brittle also, because if you are on Windows with \r\n and your message is using \n, you will still see new lines in the output even if you ask for single line.

static void WriteReplacing(TextWriter writer, string oldValue, string newValue, string message)
{
string newMessage = message.Replace(oldValue, newValue);
writer.Write(newMessage);
}

So in my project, I had to copy/paste SimpleConsoleFormatter here with two changes:

1st change: keep new lines in a message in single line mode

https://github.com/lunet-io/lunet/blob/1c01e354a0295b2c8835a973be80049c793b2fab/src/Lunet.Core/Helpers/SimpleConsoleFormatter.cs#L143-L144

2nd change: Fix the issue with messages having \n on Windows (note, this might be related to #40315)

https://github.com/lunet-io/lunet/blob/1c01e354a0295b2c8835a973be80049c793b2fab/src/Lunet.Core/Helpers/SimpleConsoleFormatter.cs#L154-L166

So I would like to know if it would be possible to maybe add a new SimpleConsoleFormatterOptions that would be:

public bool KeepNewLinesInMessage {get;set;}

Or something replacing the SingleLine option with a tree state enum?

What do you think?

cc: @maryamariyan

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions