dotnet / dotnet/dotnet-api-docs

Console logging doesn't work unless there's a delay

Open
#7,904 6 comments 1 reaction 0 assignees View on GitHub
area-Extensions-Logging help wanted Pri3 untriaged
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

```cs
using System.Text;
using System.Diagnostics;

using var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Warning)
.AddConsole();
});

ILogger logger = loggerFactory.CreateLogger();
logger.LogInformation("Example log message");

//await Task.Delay(100);

Environment.Exit(10);
```

The above does not print anything unless you uncomment the delay. I wasted an hour of time trying to figure out why logging wasn't working. A console does not buffer output. What happens if my process crashes and the last few log lines are lost?

For the record, I'm just making a .net console app that would be running as a systemd service, and systemd would capture the stdout. No reason to delay the output.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.