简体   繁体   中英

.Net Core Console Logging not appearing

Found this article on .Net logging in a console app but it's not working for me. The sample log doesn't appear in the console.

I am using the latest logging packages

The References

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.7" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.7" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.7" />
  </ItemGroup>

The Code

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

class Program
{
    static void Main(string[] args)
    {
        // instantiate DI and configure logger
        var serviceProvider = new ServiceCollection()
            .AddLogging(cfg => cfg.AddConsole())
            .AddTransient<Program>()
            .Configure<LoggerFilterOptions>(cfg => cfg.MinLevel = LogLevel.Information)
            .BuildServiceProvider();
        // get instance of logger
        var logger = serviceProvider.GetService<ILogger<Program>>();
        // use the logger
        logger.LogInformation("Woo Hooo");
    }
}

在此处输入图片说明

Your configuration is correct, the provider just doesn't get enough time to flush the output before you exit the main thread:

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

class Program
{
    static void Main(string[] args)
    {
        // instantiate DI and configure logger
        var serviceProvider = new ServiceCollection()
            .AddLogging(cfg => cfg.AddConsole())
            .AddTransient<Program>()
            .Configure<LoggerFilterOptions>(cfg => cfg.MinLevel = LogLevel.Information)
            .BuildServiceProvider();
        // get instance of logger
        var logger = serviceProvider.GetService<ILogger<Program>>();
        logger.LogInformation("Woo Hooo");

        // This will allow enough time to flush
        Console.ReadLine();
    }
}

在此处输入图片说明

Additionally, I do not think that you need .AddTransient<Program>() unless you intend on creating more than one Program instance.

Install the Microsoft.Extensions.Logging.Console NuGet package and try this:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");

        ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
            builder.AddFilter("*.Program", LogLevel.Information)
                   .AddConsole());

        ILogger logger = loggerFactory.CreateLogger<Program>();
        logger.LogInformation("Woo Hooo");

        Console.ReadLine();
    }
}

This is how logging fundamentally works in .NET Core 3.

Or using a service provider:

class Program
{
    static void Main(string[] args)
    {
        ServiceProvider serviceProvider = new ServiceCollection()
            .AddLogging(x => x.AddFilter("*.Program", LogLevel.Information)
                .AddConsole())
            .BuildServiceProvider();

        ILogger<Program> logger = serviceProvider.GetService<ILogger<Program>>();
        logger.LogInformation("Woo Hooo");

        Console.ReadLine();
    }
}
    var serviceProvider = new ServiceCollection()
        .AddLogging(cfg => cfg.AddConsole()
                             .AddFilter(lvl => lvl == LogLevel.Information))
        .AddTransient<Program>()
        .BuildServiceProvider();
    // get instance of logger
    var logger = serviceProvider.GetService<ILogger<Program>>();

The NuGet package Microsoft.Extensions.Logging write to the console from a background thread : https://github.com/dotnet/extensions/blob/v3.1.7/src/Logging/Logging.Console/src/ConsoleLoggerProcessor.cs

The code is open source, then don't hesitate to open the source.

In your code, the program close and the background thread is killed before it write the log. You can add a sleep to wait the log message :

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

class Program
{
    static void Main(string[] args)
    {
        // instantiate DI and configure logger
        var serviceProvider = new ServiceCollection()
            .AddLogging(cfg => cfg.AddConsole())
            .AddTransient<Program>()
            .Configure<LoggerFilterOptions>(cfg => cfg.MinLevel = LogLevel.Information)
            .BuildServiceProvider();
        // get instance of logger
        var logger = serviceProvider.GetService<ILogger<Program>>();
        // use the logger
        logger.LogInformation("Woo Hooo");
        Thread.Sleep(TimeSpan.FromSeconds(1));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM