Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging and configuration for .Net core 2.0 console application?

Tags:

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());             }         }     } } 
like image 933
ca9163d9 Avatar asked Aug 28 '17 20:08

ca9163d9


People also ask

Which logging framework is best for .NET Core?

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.

What is logging in .NET Core?

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).

How do I inject a logger in .NET Core?

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.

How to configure logging in ASP NET Core?

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.

What is the best way to get console logs from NET Core?

.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.

Can we use ASP NET Core config in console apps?

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?

How do I enable logging in the console app?

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


1 Answers

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.

like image 142
Kirk Larkin Avatar answered Oct 08 '22 00:10

Kirk Larkin