The following code got the errors. What's the right way to setup logging and configuration management for .Net Core 2.0 console application?
Error CS1061 'LoggerFactory' does not contain a definition for 'AddConsole' and no extension method 'AddConsole' accepting a first argument of type 'LoggerFactory' could be found (are you missing a using directive or an assembly reference?)
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()); } } } }
NLog is one of the most popular, and one of the best-performing logging frameworks for . NET. Setting up NLog is fairly simple. Developers can use Nuget to download the dependency, then edit the NLog.
In ASP.NET Core, logging providers store the logs. You can configure multiple logging providers for your application. The default ASP.NET Core configures the following logging providers: Console, Debug, EventSource, and EventLog (on Windows).
So, go to the Startup. cs file and add the ILoggerFactory parameter in the Configure() method. Then, call the AddFile() extension method to add Serillog file provider, as shown below. ASP.NET Core dependency injection will automatically pass an instance of the LoggerFactory for this parameter.
Configure logging. Logging configuration is commonly provided by the Logging section of appsettings. ... Test the settings when using an app created with the ASP.NET Core web application templates. The dotnet run command must be run in the project directory after using set.
.Net Core comes with some of these providers out of the box, for example the nuget package Microsoft.Extensions.Logging.Console will add one such provider that outputs the logs to console. There’s providers for most logging frameworks, for example Serilog, NLog, Log4Net, etc.
In .net Core 1 we could do this: And that gave use the Configuration object that we could then use in our console app. All examples for .net core 2.0 seem to be tailored to the new way Asp.Net core config is created. What is the way to create configurations for console apps?
To use in the console app the logging functionality and to resolve the dependencies from the services project the simplest way is to use Microsoft extensions logging (asp.net core DI). For simplicity reasons, all the setup from the following examples is done in Program.cs
It looks you might be missing a couple of dependencies:
Microsoft.Extensions.Logging.Console
, which provides the AddConsole
extension method.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With