Home > front end >  .net6 asp : Default output logs are formatted as json when run inside container
.net6 asp : Default output logs are formatted as json when run inside container

Time:11-25

I've just upgraded my asp web api project from net5 to net6 without touching a single piece of code. Everything works fine but I noticed that when runned inside a container, the log output is displayed as a concatenated series of Json objects instead of the expected console format.

Before

info: Microsoft.Hosting.Lifetime[14]
  Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[14]
  Now listening on: http://localhost:5000

After

{"EventId":14,"LogLevel":"Information","Category":"Microsoft.Hosting.Lifetime","Message":"Now listening on: https://[::]:5001","State":{"Message":"Now listening on: https://[::]:5001","address":"https://[::]:5001","{OriginalFormat}":"Now listening on: {address}"}}
{"EventId":14,"LogLevel":"Information","Category":"Microsoft.Hosting.Lifetime","Message":"Now listening on: http://[::]:5000","State":{"Message":"Now listening on: http://[::]:5000","address":"http://[::]:5000","{OriginalFormat}":"Now listening on: {address}"}}

I don't know if the problem comes from a modification in the actual log structure being sent to the std or from the container engine (docker in my case).

It works fine in console outside container.

Any ideas ?

CodePudding user response:

Apparently, the default format for console logging has changed from 'Simple' to 'Json'.

You can change it back by adding this line to your Dockerfile (somewhere where it ends up in the final image if you do a multi-stage build):

ENV Logging__Console__FormatterName=Simple

You can't change it in your appsettings.json file, since Microsoft set the environment variable in their Docker images which will override any settings in your config files. You have to set the environment variable.

There's an open issue on the change here: https://github.com/dotnet/dotnet-docker/issues/3274

  • Related