Search code examples
dockerserilog.net-5

Serilog .NET CORE 5 Web API - Show In Docker Logs


I have an extremely simple web api created with .NET Core 5 which is set to host in a linux Docker container. All I want to do is have my console output go into the Docker logs, like it has in the past versions of .NET Core.

Here is my Main method in Program.cs

var LOG_EVENT_LEVEL = Environment.GetEnvironmentVariable("LOG_EVENT_LEVEL");
var logEventLevel = LOG_EVENT_LEVEL != null
    ? Enum.Parse<LogEventLevel>(LOG_EVENT_LEVEL)
    : LogEventLevel.Information;

Log.Logger = new LoggerConfiguration()
    .WriteTo.Console(logEventLevel)
    .CreateLogger();

Log.Information("START");

I would think that the output "START" should be shown in my Docker logs, but it isn't. I see them in the debug window of VS as expected. Has something changed in .NET Core 5?


Solution

  • So, it turns out Visual Studio highjacks the standard output of the container while debugging. If you manually start the container on your machine, you will see the output in the logs as normal.