How to take advantage of logging in .net Core 2.2 console applications
This is related my prior article on dependency injection in .net core 2.2 console apps, and configuration in .net core 2.2. console apps. I am assuming you’ve read that before coming here. Otherwise this will make no sense.
As previously stated, we established a basic console application which can now use dependency injection using the .net core framework (2.2 or later) as well as read from various configuration files. In this article, we want to add Logging. In a final article, I will go into detail on getting Entity Framework Core spun up in a console application.
So, as before, begin with the code we had at the end of the last article. If you don’t have it, go back and re-read and create it. I may eventually put a github repository out there but as of today, I haven’t. If you don’t do this, the article will be of limited use.
So let’s get going. We need to add several NuGet packages to the ones we’ve previously incorporated. If you do not know how to do this, you should probably stop reading now.
Add the following packages in addition to the ones we already have:
- Microsoft.Extensions.Logging
- Microsoft.Extensions.Logging.Console
I’m going to log to the console in this article, but you can use other packages, such as EventLog, Log4Net and so on.
Let’s tackle logging first. We are going to take advantage of the configuration file we set up last article, and grab our logging settings from there. So update your appSettings.json file by adding a logging section as follows:
"Logging": {
"LogLevel": {
"Default": "Information"
}
},We are adding a general Logging section to the configuration file, and setting the default to Information, so anything with a level of “Information” or higher will show up on the console. If you set this to “Critical” only critical log entries will be logged. The enumeration LogLevel contains a list of all the logging types.
Now, to set it up in code, we need to first modify our ConfigureServices method that we created on our Program class. We will need to add logging to our services. When we do, we will also specify the configuration section to use and which logger we want to use. We will also set a default logging level, but the one from the configuration file will override it.
Our updated ConfigureServices method will look like:
private static IServiceCollection ConfigureServices()
{
IServiceCollection services = new ServiceCollection();// Set up the objects we need to get to configuration settings
var config = LoadConfiguration();
services.AddLogging(logging =>
{
logging.AddConfiguration(config.GetSection("Logging"));
logging.AddConsole();
}).Configure<LoggerFilterOptions>(options=>options.MinLevel=
LogLevel.Information); // Add the config to our DI container for later user
services.AddSingleton(config); services.AddTransient<ITestService, TestService>(); // IMPORTANT! Register our application entry point
services.AddTransient<ConsoleApplication>(); return services;
}So now, we are all set up for logging. All we have to do is inject a logger into our service and log away.
So, by way of example, we will add logging to our test service as follows:
public interface ITestService
{
void DoSomethingUseful();
}public class TestService : ITestService
{
private readonly ILogger<TestService> _logger;
public TestService(ILogger<TestService> logger)
{
_logger = logger;
} public void DoSomethingUseful()
{
_logger.LogInformation($"Log this");
}
}So there you go! Boom. Logging in .net core in a console application. We can now go and prosper.
Next time, Entity Framework Core






