在 Docker 容器中登录 ASP.NET Core/.NET Core 控制台应用程序 [英] ASP.NET Core/.NET Core Console app logging in Docker container

查看:23
本文介绍了在 Docker 容器中登录 ASP.NET Core/.NET Core 控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 docker 容器内运行的 .net 核心应用程序中编写日志,以便日志消息显示在 docker logs 或 Kitematic UI 中?

How do I write log in .net core application running inside docker container so that log messages will show up in docker logs <container-id> alternatively in Kitematic UI?

我几乎尝试了所有方法,但最终总是没有来自 dotnet 应用程序的日志.我在 NODE.js、nginx、rabbitmq 等容器中的所有其他非 dotnet 应用程序都可以毫无问题地写入日志.

I've tried almost everything but always end up with no logs from dotnet apps. All the others non-dotnet apps I have in containers like NODE.js, nginx, rabbitmq write logs with no problem.

这是我已经尝试过的:

  • 使用 Console.WriteLine
  • 的控制台应用程序 (.NET Core)
  • 使用 ILogger 接口的 ASP.NET Core 应用程序(使用支持 docker 的 VS 模板后的默认设置)
  • 在控制台/ASP.NET Core 应用程序中使用 log4net(日志记录实际上适用于例如使用 RollingFileAppender)和 ConsoleAppender

我找不到任何遇到相同问题的人(stackoverflow、google、github 问题),所以我想我在这里遗漏了一些重要的东西.

I couldn't find anyone experiencing the same problems (stackoverflow, google, github issues) so I assume I am missing something essential here.

这是我当前的设置:

  • Windows 10 专业版
  • Docker for Windows 17.12.0-ce
  • docker-compose 版本 1.18.0,构建 8dd22a96
  • 运行 Linux 容器

工作 Dockerfile 示例(NodeJS 应用程序)

Example of working Dockerfile (NodeJS app)

FROM node:9-slim
WORKDIR /app
EXPOSE 80

ENV NODE_PATH=/node_modules
ENV PATH=$PATH:/node_modules/.bin

COPY ./MyApp ./
RUN npm install
RUN npm run build
CMD [ "npm", "start" ]

不工作的 Dockerfile 示例(ASP.NET Core,由 Visual Studio 生成)

Example of NOT working Dockerfile (ASP.NET Core, generated by Visual Studio)

FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY *.sln ./
COPY DockerLoggingTest2/DockerLoggingTest2.csproj DockerLoggingTest2/
RUN dotnet restore
COPY . .
WORKDIR /src/DockerLoggingTest2
RUN dotnet build -c Release -o /app

FROM build AS publish
RUN dotnet publish -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "DockerLoggingTest2.dll"]

如您所见,它没有什么特别之处.所以问题一定出在 .NET Core 上,因为所有其他类型的应用程序都可以记录.

As you can see, there is nothing special about it. So the problem must be in .NET Core, since all the other types of application logs okay.

推荐答案

正如我所料.Visual Studio 是吞下所有日志消息的中间人.我认为这与 Visual Studio 调用以启用所有调试功能的 docker-compose 命令中的 yaml 覆盖有关.

It is as I suspected. Visual Studio is the man in the middle that swallows all the log messages. I think it has something to do with yaml overrides in docker-compose command that Visual Studio calls to probably enable all the debugging features.

docker-compose  -f "docker-compose.yml" -f "docker-compose.override.yml" -f "objDockerdocker-compose.vs.debug.g.yml" -p dockercompose2788718473649893946 up -d --force-recreate --remove-orphans

看起来像以下文件 objDockerdocker-compose.vs.debug.g.yml 负责我遇到的行为.当我在没有它的情况下运行此命令时,一切都按预期工作.

It looks like following file objDockerdocker-compose.vs.debug.g.yml is responsible for the behaviour I am experiencing. When I run this command without it, everything work as expected.

感谢大家的头脑风暴导致这个答案.

Thank you all for the brainstorming that led to this answer.

这篇关于在 Docker 容器中登录 ASP.NET Core/.NET Core 控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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