Skip to content

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

@kjpgit

Description

@kjpgit
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<Program>();
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.

Activity

ghost added
untriagedNew issue has not been triaged by the area owner
on Mar 24, 2022
baronfel

baronfel commented on Mar 25, 2022

@baronfel
Member

I'd agree with that: Environment.Exit per its docs doesn't wait for thread to finish:

Exit terminates an application immediately, even if other threads are running. If the return statement is called in the application entry point, it causes an application to terminate only after all foreground threads have terminated.

kjpgit

kjpgit commented on Mar 27, 2022

@kjpgit
Author

You can transfer the issue if it's appropriate. My question is, is it documented that the console logger buffers / delays the output? Is there a way to turn it off? What are the failure modes when the buffer grows?

transferred this issue fromdotnet/sdkon Mar 31, 2022
ghost

ghost commented on Apr 1, 2022

@ghost

Tagging subscribers to this area: @dotnet/area-extensions-logging
See info in area-owners.md if you want to be subscribed.

Issue Details
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<Program>();
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.

Author: kjpgit
Assignees: -
Labels:

untriaged, area-Extensions-Logging, Area-NetSDK

Milestone: -
transferred this issue fromdotnet/runtimeon Apr 1, 2022
added
help wantedGood for community contributors to help [up-for-grabs]
on Apr 1, 2022
added
Pri3Indicates issues/PRs that are low priority
on Apr 1, 2022
maryamariyan

maryamariyan commented on Apr 27, 2022

@maryamariyan
Contributor

That's by design. Keeping open and up for grabs in the dotnet-api-docs to track adding to docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Pri3Indicates issues/PRs that are low priorityarea-Extensions-Logginghelp wantedGood for community contributors to help [up-for-grabs]untriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @baronfel@maryamariyan@WeihanLi@kjpgit@jeffschwMSFT

        Issue actions

          Console logging doesn't work unless there's a delay · Issue #7904 · dotnet/dotnet-api-docs