How to make console logger in .net core 3.1 console app work

So that logging doesn't adversely impact the performance of your program, it may be written asynchronously.

Disposing the log provider and other logging classes should cause the log to flush.

The service provider should also dispose all services when it is disposed.


I might be late but worth adding some inputs in case it helps. I was also struggling with this logging in .net core and keep having breaking changes with the latest release. However, can't complain as it keeps getting better and better. Here is what I have done with the .net core 5 released.

public static class ApplicationLogging
{
    public static ILoggerFactory LogFactory { get; } = LoggerFactory.Create(builder =>
    {
        builder.ClearProviders();
        // Clear Microsoft's default providers (like eventlogs and others)
        builder.AddSimpleConsole(options =>
            {
                options.IncludeScopes = true;
                options.SingleLine = true;
                options.TimestampFormat = "hh:mm:ss ";
            }).SetMinimumLevel(LogLevel.Warning);
    });

    public static ILogger<T> CreateLogger<T>() => LogFactory.CreateLogger<T>();
}

static void Main(string[] args)
{
    var logger = ApplicationLogging.CreateLogger<Program>();
    logger.LogInformation("Let's do some work");
    logger.LogWarning("I am going Crazy now!!!");
    logger.LogInformation("Seems like we are finished our work!");
    Console.ReadLine();
}