In this post (part of a series around docker basics) I’ll be disucssing how you can view logs for running docker containers.
As usual with my posts, there’s very little here that you can’t get from the the docs - although I’d like to think these posts are slightly more accessible (especially for myself).
Viewing Logs
In fact, the process of viewing logs is remarkably easy; once you’ve launched the container, you need the ID:
docker ps
Once you have the container ID, you can feed it into the docker container logs command:
docker container logs --follow dcd204c97421
The follow argument basically leaves a session open, and updates with any new log entries (leave it out for a static view of the logs).
The docker desktop app actually provides the same functionality out of the box (but provides you with a nice search and formatting facility).
Writing Logs
Obviously, this varies from language to language, so I’ll stick to .Net here. It’s actually very straightforward:
builder.Services.AddLogging();
You can now log in the same way as you would for any other .net application; simply inject into the constructor:
public MyService(
ILogger<MyService> logger)
{
\_logger = logger;
}
And then log as you would normally:
\_logger.LogWarning("something happened");