Register and configure for .Net core 2.0 console application?

The following code received errors. What is the correct way to configure logging and configuration management for a .Net Core 2.0 console application?

Error CS1061 "LoggerFactory" does not contain a definition for "AddConsole", and the "AddConsole" extension method cannot be found that accepts the first argument of type "LoggerFactory" (do you miss the using directive or assembly references?)

Error CS1503 Argument 2: Cannot convert from "Microsoft.Extensions.Configuration.IConfigurationSection" to "System.Action"

class Program
{
    static void Main(string[] args)
    {
        var services = new ServiceCollection();
        ConfigureServices(services);
        var serviceProvider = services.BuildServiceProvider();
        var app = serviceProvider.GetService<Application>();
        Task.Run(() => app.Run()).Wait();
    }

    private static void ConfigureServices(IServiceCollection services)
    {
        ILoggerFactory loggerFactory = new LoggerFactory()
            .AddConsole() // Error!
            .AddDebug();

        services.AddSingleton(loggerFactory); // Add first my already configured instance
        services.AddLogging(); // Allow ILogger<T>

        IConfigurationRoot configuration = GetConfiguration();
        services.AddSingleton<IConfigurationRoot>(configuration);

        // Support typed Options
        services.AddOptions();
        services.Configure<MyOptions>(configuration.GetSection("MyOptions")); // Error!

        services.AddTransient<Application>();
    }

    private static IConfigurationRoot GetConfiguration()
    {
        return new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddXmlFile("App.config", optional: true).Build();
    }

    public class MyOptions
    {
        public string Name { get; set; }
    }

    public class Application
    {
        ILogger _logger;
        MyOptions _settings;

        public Application(ILogger<Application> logger, IOptions<MyOptions> settings)
        {
            _logger = logger;
            _settings = settings.Value;
        }

        public async Task Run()
        {
            try
            {
                _logger.LogInformation($"This is a console application for {_settings.Name}");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
            }
        }
    }
}
+14
source share
2 answers

, :

.NET Core, Andrew Lock .NET Core, .NET Core 2, .

+12

(, nuget, Microsoft.Extensions.Options.ConfigurationExtensions, Microsoft.Extensions.Logging.Console Microsoft.Extensions.Logging.Debug), loggerFactory, ILoggerBuilder .AddLogging():

public static IServiceProvider ConfigureServices(IServiceCollection serviceCollection)
{
    //ILoggerFactory loggerFactory = new LoggerFactory()
    //  .AddConsole()
    //  .AddDebug();

    serviceCollection
        .AddLogging(opt =>
        {
            opt.AddConsole();
            opt.AddDebug();
        })
        .AddTransient<IFooService, FooService>();

    /*... rest of config */

    var serviceProvider = serviceCollection.BuildServiceProvider();
    return serviceProvider;
}
+9

Source: https://habr.com/ru/post/1684635/


All Articles