Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core/.NET Core Console app logging in Docker container

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.

like image 304
Radek Stromský Avatar asked Mar 08 '18 06:03

Radek Stromský


People also ask

How do I run a dotnet core console application on Docker?

If you just want to run app in docker, build your app with visual studio on you local machine (this will be the most easy way to build it), use the microsoft/dotnet:2.1-runtime-nanoserver-1709 , build a docker image. Save this answer.

Can you Containerize .NET application?

Containerizing a . NET application is easy. You can do this by copying source code files and building a Docker image. We'll also cover common concerns like image bloat, missing image tags, and poor build performance with these nine tips for containerizing your .


2 Answers

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.

like image 130
Radek Stromský Avatar answered Oct 12 '22 17:10

Radek Stromský


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

like image 9
Jared Onnen Avatar answered Oct 12 '22 18:10

Jared Onnen