Logging and configuration for .Net core 2.0 console application?

It looks you might be missing a couple of dependencies:

  1. Microsoft.Extensions.Logging.Console, which provides the AddConsole extension method.
  2. Microsoft.Extensions.Options.ConfigurationExtensions, which provides the Configure<T> extension method you appear to be missing.

In terms of configuring the services in .NET Core executables, Andrew Lock has a post on the topic for the first version of .NET Core. Some of this may be out of date now with .NET Core 2's recent arrival, but it's worth a read.


Building upon Kirk Larkins answer (make sure you have the nuget packages installed, Microsoft.Extensions.Options.ConfigurationExtensions, Microsoft.Extensions.Logging.Console and Microsoft.Extensions.Logging.Debug) then you can simplify the logging configuration and skip the declaration of the loggerFactory by passing in an ILoggerBuilder to .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;
}

Tags:

C#

.Net

.Net Core