.net6 asp:在容器内运行时,默认输出日志格式为 json [英] .net6 asp : Default output logs are formatted as json when run inside container

查看:17
本文介绍了.net6 asp:在容器内运行时,默认输出日志格式为 json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

之前

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).

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.

有什么想法吗?

推荐答案

显然,控制台日志记录的默认格式已从简单"更改为Json".

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

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

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

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

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.

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

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

这篇关于.net6 asp:在容器内运行时,默认输出日志格式为 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆