Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
633 views
in Technique[技术] by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...