`dotnet watch` removes console color

See original GitHub issue

Describe the bug

Whatever changed with how the console output is redirected in preview 8 causes the colors and extra logging to go missing. The below output compares running dotnet run and dotnet watch run:

image

To Reproduce

Create a new blank hosted project:

dotnet new blazorwasm --hosted

Then run both dotnet run and dotnet watch run on the Server project. Notice there are no colors with watch as well as less output.

For example, I can add this to the WeatherForecastController.Get() method:

_logger.LogWarning("Hope it's not too hot!");

Now view the difference in output when navigating to the Fetch Data page:

image

Further technical details

  • Include the output of dotnet --info
.NET SDK (reflecting any global.json):
 Version:   5.0.100-preview.8.20417.9
 Commit:    fc62663a35

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19041
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.100-preview.8.20417.9\

Host (useful for support):
  Version: 5.0.0-preview.8.20407.11
  Commit:  bf456654f9

.NET SDKs installed:
  3.1.103 [C:\Program Files\dotnet\sdk]
  3.1.202 [C:\Program Files\dotnet\sdk]
  3.1.401 [C:\Program Files\dotnet\sdk]
  5.0.100-preview.8.20417.9 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0-preview.7.20365.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0-preview.8.20414.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0-preview.7.20364.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0-preview.8.20407.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0-preview.7.20366.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0-preview.8.20411.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download
  • The IDE (VS / VS Code/ VS4Mac) you’re running on, and it’s version

Tested in Windows Terminal and VS Code terminal.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:24
  • Comments:19 (6 by maintainers)

github_iconTop GitHub Comments

13reactions
zbecknellcommented, Dec 28, 2020

Alright, so I finally had time to dig into this.

It’s the browser launch specifically that’s the culprit. This is because the browser launch depends on reading the standard output from the underlying Process (as you discovered @rabberbock).

When the process is started if it has any handler attached to OnOutput it must set RedirectStandardOutput to true:

https://github.com/dotnet/aspnetcore/blob/cd5303d6d82b067ec5365970229fe34588f924d0/src/Tools/dotnet-watch/src/Internal/ProcessRunner.cs#L102

When this happens we lose our colors. The DataReceivedEventHandler.Data property only contains text with no color info. At the same time, we can’t just “look” at the output and still have it appear as it normally would in the console.

What to do?

Given the way this works right now, we cannot have colors and launch the browser (unless there’s a way to stop redirecting output when the process has already started, which I didn’t find).

The downside is that we also lose the excellent auto-refresh feature, which as far as I can tell could run completely separately from the browser launching. So my proposal for a fix is to simply decouple the two and allow the refresh server to launch without also needing the browser to launch.

I am submitting a simple PR that just moves the refresh server code out of the if statement that launches the browser.

8reactions
tesar-techcommented, May 3, 2022

Any news for upcoming .net 7?

Read more comments on GitHub >

github_iconTop Results From Across the Web

dotnet watch command - .NET CLI
The dotnet watch command is a file watcher that runs a dotnet command when changes in source code are detected.
Read more >
Silence all warnings in dotnet cli output
Especially as docker-compose is removing the color from the output, so the errors are hard to distinguish from warnings. I realize this setup...
Read more >
NET environment variables - .NET CLI
When Console.IsOutputRedirected is true , you can emit ANSI color code by setting DOTNET_SYSTEM_CONSOLE_ALLOW_ANSI_COLOR_REDIRECTION to either 1 ...
Read more >
Console.ResetColor Method (System)
Sets the foreground and background console colors to their defaults. ... It then changes the foreground color to each color in the ConsoleColor...
Read more >
.NET 6 Blazor Hot Reload fails depending on the port ...
NET 6 I created a brand new Blazor project. From a terminal I then launched the application using dotnet watch . However, when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found