Logging in .Net core console application not working

前端 未结 3 1854
孤街浪徒
孤街浪徒 2021-01-02 19:07

I am following this tutorial: https://andrewlock.net/using-dependency-injection-in-a-net-core-console-application/

and accordingly installed the packages but log is

相关标签:
3条回答
  • 2021-01-02 19:32

    I landed on this thread trying to troubleshoot why console logging didn't work and this answer documents what I found. Packages used: Microsoft.Extensions.Logging Microsoft.Extensions.Logging.Console Microsoft.Extensions.Logging.Debug

    Application: .NET Core 2.2 Console (Microsoft.NET.Sdk, netcoreapp2.2) Using Microsoft.Extensions.Hosting.IHost, this is how I added console logging:

    var hostBuilder = new HostBuilder()
                    // Other Configuration omitted for brevity
                    .ConfigureLogging((hostBuilderContext, loggingBuilder) =>
                    {
                        loggingBuilder.AddConfiguration(hostBuilderContext.Configuration.GetSection("Logging"));
                        loggingBuilder.AddConsole(options =>
                        {
                            options.IncludeScopes = true;
                        });
                        loggingBuilder.AddDebug();
                    });
    // Start the application
    await hostBuilder.RunConsoleAsync();
    

    Interestingly, if I remove the options parameter in the call to AddConsole, I do not see any logging. I believe this is so because I use an ILogger in my code that emits log statements:

    public class ClassThatLogs
    {
        private readonly ILogger<ClassThatLogs> _logger;
    
        public ClassThatLogs(ILogger<ClassThatLogs> logger)
        {
            _logger = logger;
        }
    
        public void DoWork()
        {
            _logger.LogInformation("Working");
        }
    }
    
    0 讨论(0)
  • 2021-01-02 19:44

    Turns out the console logging provider doesn't immediately flush the messages to the console like it did in the net-core-1.x versions. It appears to run on a different thread. See this web page for info: https://github.com/aspnet/Logging/issues/631

    You can add at the end of the Main function.

    serviceProvider.Dispose();
    

    or you can add .AddDebug()

                serviceProvider
                .GetService<ILoggerFactory>()
                .AddConsole(LogLevel.Debug)
                .AddDebug();
    
    0 讨论(0)
  • 2021-01-02 19:44

    Creating a new ServiceProvider and HostBuilder may not be worth it if we just want a Logging in Console Application because it's a bit of extra caution to clean it up or dispose of. Rather, I would suggest just have Logging Factory to use logger and that will solve the logging if that is only what we want.

    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 ";
            });
            builder.AddApplicationInsights("instrument-key");
        });
        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();
    }
    
    0 讨论(0)
提交回复
热议问题