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

net5.0 project has no color on console output with dotnet watch run #229

Closed
jmalatia opened this issue Jan 23, 2021 · 4 comments
Closed
Labels

Comments

@jmalatia
Copy link

After upgrading a project to net5.0 (5.0.1), Serilog console output colors stopped working.

After playing around for while I experienced:

  • If I do a dotnet watch run --environment "Development" no color (this is our normal usage in our development flow and colors always showed in netcore2.2, netcore3.1 apps)
  • If I do a dotnet run --environment "Development" everything is in color as expected
  • Using Visual Studio > Start Debugging > The console output is in color.

Setup:

appsettings.json

{
  "Serilog": {
    "MinimumLevel": {
      "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
      "Default": "Information",
      "Override": {
        "Microsoft.AspNetCore": "Warning"
      }
    },
    "Enrich": [
      "FromLogContext"
    ],
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Literate, Serilog.Sinks.Console",   // tried with and without this line with no changes
          "applyThemeToRedirectedOutput": true,   // tried with and without this line with no changes
          "outputTemplate": "[{Timestamp:hh:mm:ss tt} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}"  // tried with and without this line with no changes
        }
      }
    ]
  }
}

program.cs

public static IHostBuilder CreateHostBuilder(string[] args)
        {
            Environment.SetEnvironmentVariable("LOGDIR", AppContext.BaseDirectory);

            return Host.CreateDefaultBuilder(args)
                .UseSerilog((hostingContext, loggerConfiguration) => 
                        loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration)
                )
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
        }

startup.cs

     app.UseSerilogRequestLogging();

Tried using Serilog.AspNetCore 3.4.0 and Serilog.AspNetCore 3.4.1-dev-00188

@jmalatia jmalatia added the bug label Jan 23, 2021
@nblumhardt
Copy link
Member

Thanks! This appears to be an instance of dotnet/aspnetcore#25317

@jmalatia
Copy link
Author

Thanks for the link @nblumhardt!

Changed my launchSettings.json removing the entry launchBrowser and the colors returned.

@johnnyreilly
Copy link

Thanks @jmalatia - that fix worked for me too

@iappwebdev
Copy link

So this solution did not work for me, but was worked was to set applyThemeToRedirectedOutput to true in the logger configuration. I found this solution here: color themes not appearing in output

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

Or in appsettings.json:

"WriteTo": [
  {
    "Name": "Console",
    "Args": {
      "applyThemeToRedirectedOutput": true,
      "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Literate, Serilog.Sinks.Console",
      "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}   [{SourceContext}]{NewLine}{Exception}"
    }
  }
]

Read the configuration:

services.AddSerilog((hostingContext, loggerConfiguration) => loggerConfiguration.ReadFrom.Configuration(configuration));

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

No branches or pull requests

4 participants