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.WriteLine
I couldn't find anyone experiencing the same problems (stackoverflow, google, github issues) so I assume I am missing something essential here.
This is my current setup:
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.
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.
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 .
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With