ASP.NET Core/.NET Core Console app logging in Dock

2020-06-04 05:12发布

问题:

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?

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.

Here is what I've already tried:

  • console app (.NET Core) using Console.WriteLine
  • ASP.NET Core app (default setup after using VS template with docker support) using ILogger interface
  • using log4net inside console/ASP.NET Core app (logging actually works for example with RollingFileAppender) with ConsoleAppender

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

UPDATE 1:

This is my current setup:

  • Windows 10 Pro
  • Docker for Windows 17.12.0-ce
  • docker-compose version 1.18.0, build 8dd22a96
  • running Linux containers

UPDATE 2:

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" ]

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"]

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.

回答1:

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 "obj\Docker\docker-compose.vs.debug.g.yml" -p dockercompose2788718473649893946 up -d --force-recreate --remove-orphans

It looks like following file obj\Docker\docker-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.



回答2:

Adding the following to appsettings.json

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "System": "Information",
      "Microsoft": "Information"
    },
    "Console": {
      "IncludeScopes": true
    }
  }

be sure your project is configured to copy your appsettings, inside of YourProject.csproj ensure

  <ItemGroup>
    <Content Include="appsettings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

I then added the config to the logging service.

            services.AddLogging(builder =>
                builder
                    .AddDebug()
                    .AddConsole()
                    .AddConfiguration(configuration.GetSection("Logging"))
                    .SetMinimumLevel(LogLevel.Information)
            );

hope this helps