Different and multiple logging outputs

Viewed 50

I have created an ASP.NET Core 3 application with the API template. I've added an information logging row in WeatherForecastController:

_logger.LogInformation("HELLO!");

The console output of this row is is different depending on where how I run the application.

When I run the application with dotnet run in Windows Terminal:

info: WebApplication1.Controllers.WeatherForecastController[0]
      HELLO!

When I run in debug mode in Visual Studio 2019:

WebApplication1.Controllers.WeatherForecastController: Information: HELLO!

When I run in debug mode in Visual Studio Code I get both variants at the same time:

info: WebApplication1.Controllers.WeatherForecastController[0]
      HELLO!
WebApplication1.Controllers.WeatherForecastController: Information: HELLO!

The differences bugs me, but the main reason I bother is because I use Visual Studio Code and the duplicate output makes it much harder to read.

Why are the outputs different and can I somehow control this to get the same everywhere or at least only one of them in Visual Studio Code?

1 Answers

In ASP.NET default template the row Host.CreateDefaultBuilder(args) adds both DebugLogger and ConsoleLogger. Running dotnet run shows from console and Visual Studio 2019 shows from debug. Visual Studio Code shows from both.

My best solution so far is to add this to my appsettings.development.json to disable Debug logging (and only keep Console logging):

{
    "Logging": {
        "Debug": {
            "LogLevel": {
                "Default": "None"
            }
        }
    }
}
Related