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

applyThemeToRedirectedOutput prevents dotnet watch run from starting browser for web apps #156

Open
Meligy opened this issue May 5, 2024 · 2 comments

Comments

@Meligy
Copy link

Meligy commented May 5, 2024

Hello,

First, thanks a lot for the defacto standard dotnet logger that countless apps depend on.

Sorry for only getting to say this while contacting for a small-ish bug.

This bug is about how applying theme to redirected output seems to stop dotnet watch run from starting a browser if the dotnet app is a web application.

I'll use the template provided, just as headers so that I don't have to indent my input:

The exact package id and version you're using

"Serilog.Sinks.Console" v5.0.0

dotnet list package --include-transitive

Project 'RedirectedThemeSample' has the following package references
   [net8.0]: 
   Top-level Package         Requested   Resolved
   > Serilog.AspNetCore      8.0.1       8.0.1   

   Transitive Package                                           Resolved
   > Microsoft.Extensions.Configuration.Abstractions            8.0.0   
   > Microsoft.Extensions.Configuration.Binder                  8.0.0   
   > Microsoft.Extensions.DependencyInjection                   8.0.0   
   > Microsoft.Extensions.DependencyInjection.Abstractions      8.0.0   
   > Microsoft.Extensions.DependencyModel                       8.0.0   
   > Microsoft.Extensions.Diagnostics.Abstractions              8.0.0   
   > Microsoft.Extensions.FileProviders.Abstractions            8.0.0   
   > Microsoft.Extensions.Hosting.Abstractions                  8.0.0   
   > Microsoft.Extensions.Logging                               8.0.0   
   > Microsoft.Extensions.Logging.Abstractions                  8.0.0   
   > Microsoft.Extensions.Options                               8.0.0   
   > Microsoft.Extensions.Primitives                            8.0.0   
   > Serilog                                                    3.1.1   
   > Serilog.Extensions.Hosting                                 8.0.0   
   > Serilog.Extensions.Logging                                 8.0.0   
   > Serilog.Formatting.Compact                                 2.0.0   
   > Serilog.Settings.Configuration                             8.0.0   
   > Serilog.Sinks.Console                                      5.0.0   
   > Serilog.Sinks.Debug                                        2.0.0   
   > Serilog.Sinks.File                                         5.0.0   
   > System.Diagnostics.DiagnosticSource                        8.0.0   
   > System.Text.Encodings.Web                                  8.0.0   
   > System.Text.Json                                           8.0.0  

Your dotnet toolchain version, target framework, and operating system,

dotnet --info
.NET SDK:
 Version:           8.0.204
 Commit:            c338c7548c
 Workload version:  8.0.200-manifests.4b097a37

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  14.4
 OS Platform: Darwin
 RID:         osx-arm64
 Base Path:   /usr/local/share/dotnet/sdk/8.0.204/

The current behavior

I have run the following steps:

mkdir RedirectedThemeSample
cd RedirectedThemeSample
dotnet new web
dotnet add package Serilog.AspNetCore --version 8.0.1
dotnet restore

Then I modified the "Program.cs" file to be:

using Serilog;
using Serilog.Sinks.SystemConsole.Themes;

var builder = WebApplication.CreateBuilder(args);

// Only added part to the app other than `using`s
builder.Host.UseSerilog((context, services, configuration) =>
    configuration
        .WriteTo.Console(theme: AnsiConsoleTheme.Code, applyThemeToRedirectedOutput: true)
);

var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();

Then I ran:

dotnet watch run

The app raun, but the browser did not start, instead, I got a non blocking negative message in the console:

The file /Users/meligy/code/temp/RedirectedThemeSample/http:/localhost:5228 does not exist.
image

The only AnsiConsoleTheme that lets the browser open when I restart dotnet watch run is AnsiConsoleTheme.None, which loses all console colours. You also lose all console colours if you leave applyThemeToRedirectedOutput as false.

What you expect or want to happen instead

Ideally the browser would start, the console colouring would still work, and I wouldn't see the message that treats the URL as a file.

Additional Notes

It looks from the message as if the dotnet run is trying to start the URL as a process, and the redirection treats that as running an executable file or something. But I am not sure.

Again, thanks for all the great work.

Cheers,

@Meligy Meligy changed the title applyThemeToRedirectedOutput prevents starting ASP.NET Core browser applyThemeToRedirectedOutput prevents dotnet watch run from starting browser for web apps May 5, 2024
@nblumhardt
Copy link
Member

Hi @Meligy, nice to hear from you 👋

Is this a potential improvement to dotnet watch, i.e. ignoring ANSI escape sequences when looking for the Now listening on: log line?

IIRC, dotnet watch uses a simple regex to find this line, so although it could turn out to be a fairly odd looking regex, it might be reasonable just to extend it to ignore ANSI escapes. If it turns out that's the case it'd likely have benefits to other logging systems also :-)

@Meligy
Copy link
Author

Meligy commented Jun 10, 2024

Hello,

Sorry, I've just realised that this is a question not just a comment.

Is this a potential improvement to dotnet watch, i.e. ignoring ANSI escape sequences when looking for the Now listening on: log line?

This is a default behaviour of dotnet run itself actually, and dotnet watch supports it. I'm not sure how it works under the hood.

I think you might find some info about that here: dotnet/aspnetcore#25317 (comment)

In the past I remember disabling browser launch with an environment variable to allow for console colors.

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

2 participants