.net6 asp :在容器内运行时默认输出日志格式为 json

MCa*_*ier 10 containers docker .net-6.0

我刚刚将我的 asp web api 项目从 net5 升级到 net6,没有触及任何代码。一切正常,但我注意到,当在容器内运行时,日志输出显示为一系列串联的 Json 对象,而不是预期的控制台格式。

info: Microsoft.Hosting.Lifetime[14]
  Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[14]
  Now listening on: http://localhost:5000
Run Code Online (Sandbox Code Playgroud)

{"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}"}}
Run Code Online (Sandbox Code Playgroud)

我不知道问题是否来自发送到 std 的实际日志结构的修改或来自容器引擎(在我的例子中为 docker)。

它在容器外部的控制台中运行良好。

有任何想法吗 ?

Han*_*ian 23

显然,控制台日志记录的默认格式已从“Simple”更改为“Json”。

您可以通过将此行添加到您的 Dockerfile 来将其更改回来(如果您执行多阶段构建,它最终会出现在最终映像中的某个位置):

ENV Logging__Console__FormatterName=Simple
Run Code Online (Sandbox Code Playgroud)

您无法在 appsettings.json 文件中更改它,因为 Microsoft 在其 Docker 映像中设置了环境变量,这将覆盖您的配置文件中的任何设置。您必须设置环境变量。

这里的更改有一个未解决的问题:https ://github.com/dotnet/dotnet-docker/issues/3274