Summary
Highlights
This section introduces the benefits of .NET Core, such as dependency injection, a better configuration system, and common logging. It highlights the challenge of using these features in console applications, which typically receive less out-of-the-box support compared to web environments. The video aims to demonstrate how to implement these features in a console application.
The tutorial begins by creating a new .NET Core console application in Visual Studio 2019. It then guides the user through installing necessary NuGet packages: Microsoft.Extensions.Hosting for setting up the hosting environment, Serilog.Extensions.Hosting, Serilog.Settings.Configuration, and Serilog.Sinks.Console for advanced logging capabilities and configuration from appsettings.json.
This part focuses on adding and configuring the appsettings.json file. It emphasizes the importance of setting 'Copy to Output Directory' to 'Copy always' to ensure the configuration file is available during runtime. The video also explains how appsettings.json can handle environment-specific overrides (e.g., appsettings.development.json) and environmental variables, providing a flexible configuration hierarchy.
The video details the initial setup of the configuration builder and Serilog. It explains how to manually build a configuration source from appsettings.json before the main host setup. This allows early logging, demonstrating how to use the builder to read configuration settings for Serilog. The `Log.Logger` is initialized to use Serilog, configure it from the built configuration, enrich logs with context, and write them to the console.
This section explains how to use `Host.CreateDefaultBuilder` to set up the default hosting environment, which includes predefined configurations for environment, app settings, user secrets, and environmental variables. It then shows how to use `ConfigureServices` to register application services for dependency injection, including `UseSerilog` to integrate Serilog into the host's logging system.
A `GreetingService` class is created to encapsulate the application's core logic. The class's constructor is set up to receive `ILogger` and `IConfiguration` via dependency injection. The `GreetingService` includes a `Run` method that logs messages using the injected logger and retrieves configuration values (like loop times) from the `IConfiguration` object. The importance of using interfaces for services (e.g., `IGreetingService`) for testability and maintainability is also highlighted.
The video concludes by summarizing how to set up dependency injection, logging, and configuration in a .NET Core console application, bringing it to a level similar to ASP.NET Core applications. It suggests that this setup can be used as boilerplate for future projects or even integrated into custom project templates for quick development, encouraging viewers to explore more advanced uses or request related topics.
The video demonstrates how to activate and run the `GreetingService` using `ActivatorUtilities.CreateInstance` from the host's service provider. It also covers a common error related to creating instances of interfaces instead of concrete types, and how to correctly resolve this. Furthermore, it shows how to adjust Serilog's minimum logging levels in appsettings.json to control verbosity and avoid excessive logging for non-critical information.