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

Colors are always suppressed when a console is redirected #50

Closed
2 of 7 tasks
atruskie opened this issue Dec 14, 2018 · 6 comments
Closed
2 of 7 tasks

Colors are always suppressed when a console is redirected #50

atruskie opened this issue Dec 14, 2018 · 6 comments

Comments

@atruskie
Copy link

A few questions before you begin:

Is this an issue related to the Serilog core project or one of the sinks or community projects. This issue list is intended for Serilog core issues. If this issue relates to a sink or related project, please log on the related repository. Please use Gitter chat and Stack Overflow for discussions and questons.

Does this issue relate to a new feature or an existing bug?

  • Bug
  • New Feature

What version of Serilog Console Sink is affected by this issue? Please list the related NuGet package.
3.1.2-dev-00779

What is the target framework and operating system affected by this issue? Please see target frameworks & net standard matrix.

  • netCore 2.0
  • netCore 1.0
  • 4.7
  • 4.6.x
  • 4.5.x

Please describe the current behaviour you are experiencing?

When running my .NET Core console application colors are suppressed when the streams are redirected.

More importantly, colors in both streams are suppressed even if only one stream is redirected.

E.g.: MyApp.exe > test.txt redirects stdout (but not stderr) and removes all color from both stdout and stderr.

Please describe the expected behaviour if the ?

  • I'd like for my stderr output to retain color if that stream is not redirected.
  • More broadly, I'd like automatic color suppression for redirected streams to be configureable

Some context: my stdout stream produces data that is usually captured via redirection. Hence I send all logs to stderr and I'd like them to always look the same - whether or not someone is capturing stdout.

If the current behavior is a bug, please provide the steps to reproduce the issue and if possible a minimal demo of the problem

My configuration:

        private static IServiceCollection ConfigureLogging(IServiceCollection services)
        {
            Log.Logger = new LoggerConfiguration()
                .Enrich.WithThreadId()
                .MinimumLevel.Is(LogEventLevel.Verbose)
                .WriteTo.Console(
                    theme: AnsiConsoleTheme.Literate,
                    outputTemplate: "{Timestamp:o} [{Level:w5}] <{ThreadId}> {SourceContext} {Message:lj}{NewLine}{Exception}",
                    standardErrorFromLevel: LogEventLevel.Verbose)
                .CreateLogger();

            return services.AddLogging(
                (configure) =>
                {
                    configure.AddSerilog(Log.Logger, dispose: true);
                });
        }

And the line in the configuration extension that causes this beaviour:

var appliedTheme = System.Console.IsOutputRedirected || System.Console.IsErrorRedirected ?
ConsoleTheme.None :
theme ?? SystemConsoleThemes.Literate;

@nblumhardt
Copy link
Member

May be a little late for me to chime in, but thanks for the note @atruskie!

I think it's a reasonable expectation we'd do this separately per output stream, but I can't see a clean way to bake the functionality in quickly - some thought would need to go into how to restructure things.

@dahovey
Copy link

dahovey commented Jan 2, 2020

I believe I am running into this as well. I am listening to logs from Kubernetes pod running .NET Core 3.1. Whatever value I set for the theme it doesn't seem to be applied.

 "Serilog": {
    "Using": [
      "Serilog.Sinks.Console"
    ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information"
      }
    },
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "theme": "Serilog.Sinks.SystemConsole.Themes.SystemConsoleTheme::Literate, Serilog.Sinks.Console",
          "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}"
        }
      }
    ],

@nblumhardt
Copy link
Member

Hi! The most recent 4.0.0-dev package includes applyThemeToRedirectedOutput; this might help, in your case, @dahovey ? - Details in #64 - HTH!

@greggbjensen
Copy link

I am using the Kubernetes Dashboard to view logs. I have updated to Serilog.Sinks.Console 4.0.0-dev-00834 and set the appsettings.json as follows:

{
  "Serilog": {
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "[{Level:u3}]({RequestId}) {Message:lj}{NewLine}{Exception}",
          "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Literate, Serilog.Sinks.Console",
          "applyThemeToRedirectedOutput": true
        }
      }
    ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "System": "Information",
        "Microsoft": "Information",
        "Microsoft.EntityFrameworkCore.Database.Command": "Warning"
      }
    }
  }
}

I am not seeing any colors yet. Thanks for your help.

image

@f2calv
Copy link

f2calv commented Nov 30, 2020

FYI using 4.0.0-dev-00839 I have the coloured console output working on AKS with the following confguration in appsettings.json;

...
"WriteTo": [
  {
	"Name": "Console",
	"Args": {
	  "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
	  "applyThemeToRedirectedOutput": true
	}
  }
],
...

@nblumhardt
Copy link
Member

Closing as stale.

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

No branches or pull requests

5 participants