.net6 asp : 默认输出日志格式为 json when 运行 inside container

.net6 asp : Default output logs are formatted as json when run inside container

我刚刚将我的 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

之后

{"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}"}}

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

它在容器外的控制台中工作正常。

有什么想法吗?

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

您可以通过将此行添加到您的 Docker 文件(如果您进行多阶段构建,它最终出现在最终图像的某个地方)来将其改回:

ENV Logging__Console__FormatterName=Simple

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

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