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

color themes not appearing in output #64

Closed
3 of 8 tasks
WellspringCS opened this issue Jul 13, 2019 · 20 comments
Closed
3 of 8 tasks

color themes not appearing in output #64

WellspringCS opened this issue Jul 13, 2019 · 20 comments

Comments

@WellspringCS
Copy link

WellspringCS commented Jul 13, 2019

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.

Linux Ubuntu 18.04

Code for 3.0 shown here, but I replicated it in netcoreapp2.2 as well.

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="serilog.sinks.console" Version="3.1.1" />
  </ItemGroup>

</Project>

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

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

Please describe the current behaviour you are experiencing?
log output has no colorization

Please describe the expected behaviour if the ?
colorization of output, as per description

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

brand new project, didn't change Startup.cs

Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;

namespace sinks
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                .Enrich.FromLogContext()
                .WriteTo.Console()
                .CreateLogger();
            Log.Error("Hello, world!");
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
}

launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (web)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "logging": {
                "engineLogging": false,
                "moduleLoad": false,
                "exceptions": true,
                "browserStdOut": false,
                "programOutput": true
            },
            "program": "${workspaceFolder}/bin/Debug/netcoreapp3.0/sinks.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            "stopAtEntry": false,
            "serverReadyAction": {
                "action": "openExternally",
                "pattern": "^\\s*Now listening on:\\s+(https?://\\S+)"
            },
            "env": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            },
            "sourceFileMap": {
                "/Views": "${workspaceFolder}/Views"
            }
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }
    ]
}

image

Notice that the Serilog log item has no color, however the built-in logger output below it does.

@nblumhardt
Copy link
Member

Thanks! I think this will be due to the output redirection check here: https://github.com/serilog/serilog-sinks-console/blob/dev/src/Serilog.Sinks.Console/ConsoleLoggerConfigurationExtensions.cs#L60

Perhaps we consider an ignoreOutputRedirection flag or similar? Discoverability will be a problem, though.

@WellspringCS
Copy link
Author

[nods in agreement, hoping to look like someone who understands the ramifications as relate to discoverability...]

@michaeltrefry
Copy link

michaeltrefry commented Jul 16, 2019

Well, this answers the question I've been banging my head against a wall for several hours trying to solve.
When run locally in windows, I have colors.
When the log is output from a docker container on linux, and output using kubectl logs, no colors.
Are there any work arounds aside from writing my own Console sink?
I noticed all the pertinent classes are internal, so I can't even manually configure it.

@WellspringCS
Copy link
Author

Any timeline for addressing this? Thx in advance.

@nblumhardt
Copy link
Member

The code involved shouldn't be onerous, it's probably a small change - we're mostly in need of a proposal that works through alternative solutions. My concern with ignoreOutputRedirection is that it might take us further towards a bloated WriteTo.Console() argument list - @adamchester and I talked briefly about representing this in the Theme somehow as an alternative, not sure what that would look like.

@tjaart
Copy link

tjaart commented Nov 20, 2019

Would it make sense to default ignoreOutputRedirection to true ? What was the reasoning behind the check originally?

@nblumhardt
Copy link
Member

@tjaart the default avoids writing control sequences when piping the output of a command to a file (e.g. my-app > results.txt)

@tjaart
Copy link

tjaart commented Nov 21, 2019

@nblumhardt I've created a PR. I was unsure about the direction of the change, please feel free to make suggestions.

@tjaart
Copy link

tjaart commented Nov 25, 2019

@WellspringCS you can try the latest dev version of Serilog and use the applyThemeToRedirectedOutput option in Write.To.Console():

<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0-dev-00834" />

I did notice however that Rancher needs an ANSI Color scheme to show colours in output, so if you are working on that platform you have to force the theme.

  Log.Logger = new LoggerConfiguration()
                .WriteTo.Console(theme: AnsiConsoleTheme.Literate, applyThemeToRedirectedOutput: true)
                .CreateLogger();

@WellspringCS
Copy link
Author

Thank you, will do

@WellspringCS
Copy link
Author

Just had a good opportunity to implement this, but did not see any change. Am I doing something wrong?

using (Ubuntu 18.04):

<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0-dev-00834" />
Log.Logger = new LoggerConfiguration()
  .ReadFrom.Configuration(configuration) // use my settings
  .Enrich.FromLogContext()
  .WriteTo.Console(theme: AnsiConsoleTheme.Literate, applyThemeToRedirectedOutput: true)
  .CreateLogger();

This is what I see...

image

Do I need to change my versions on other Serilog package references for this to work?

@nblumhardt
Copy link
Member

Hi @WellspringCS - no, console should be enough; might need some more investigation. What terminal are you using? (Default in Ubuntu?) Is your app running under Docker or some other orchestrator that manages output? Thanks!

@WellspringCS
Copy link
Author

No orchestration, just VSC running on Ubuntu, output going straight to Debug Console in lower pane. HTH...

@thylux
Copy link

thylux commented Feb 26, 2020

Hi, I'm running on Docker and can replicate both behaviors.
If I run my docker interactively (-it) and execute the app from bash I can see the colors.
If I use the dockerfile ENTRYPOINT to start the application without a shell I don't see colors.

@tjaart I didn't try using the dev version you mentioned.
Maybe this clue will help understand this somehow.

@tjaart
Copy link

tjaart commented Feb 27, 2020

@WellspringCS have you tried setting applyThemeToRedirectedOutput to true and using the AnsiConsoleTheme instead of the AnsiConsoleTheme theme?

@tjaart
Copy link

tjaart commented Feb 27, 2020

@thylux did you set the applyThemeToRedirectedOutput value to true?

@thylux
Copy link

thylux commented Feb 27, 2020

@tjaart No. i'm using Serilog.Sinks.Console, Version=3.1.1.0.
What I'm trying to point out is that on a docker environment there's a difference between starting the app using ENTRYPOINT and executing from bash.

I don't know enough of docker to know why but that might give you some hint.

@vikasjayaswal
Copy link

The Documentation for Serilog.Sinks.Console needs to be updated to include the "applyThemeToRedirectOutput"

@iappwebdev
Copy link

iappwebdev commented Apr 10, 2024

@tjaart Thank you so much for this solution. I have spend hours to resolve this issue with a .NET Web API Application running under IIS Express. I tried several things from net5.0 project has no color on console output with dotnet watch run and dotnet watch removes console color but none of the suggestions worked.

Setting applyThemeToRedirectedOutput to true in my appsettings.json was the solution that worked for me.

@WellspringCS
Copy link
Author

@WellspringCS Thank you so much for this solution.

I can't take credit for the solution--only for opening the issue, LOL. Glad the solution offered here (not by me) worked for you, though!

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

No branches or pull requests

7 participants