.NET Core Console App with Dependency Injection, Logging, and Settings

Share

Summary

This video demonstrates how to implement dependency injection, logging with Serilog, and configuration using appsettings.json in a .NET Core console application. The tutorial covers setting up NuGet packages, configuring the appsettings.json file for dynamic settings and Serilog logging levels, and structuring the application with interfaces for better maintainability and testability.

Highlights

Introduction to .NET Core Console App Benefits
00:00:00

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.

Setting up the Project and NuGet Packages
00:01:49

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.

Configuring AppSettings.json
00:09:16

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.

Initial Configuration and Serilog Setup
00:11:06

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.

Implementing the Hosting Environment and Dependency Injection
00:25:07

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.

Creating and Injecting a Greeting Service
00:30:11

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.

Conclusion and Future Possibilities
00:55:02

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.

Running the Application and Debugging Common Issues
00:43:08

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.

Recently Summarized Articles

Loading...