Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the Logging logging component in .net Core

Shulou Source: shulou.com Published: 2022-06-02 05:31:21 10月21日 Update

. Net Core how to use Logging logging components, I believe that many inexperienced people are helpless about this, this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Create a new. Net Core Web Api project and add the following code:

The results are as follows:

When I first got involved, I had a question--I didn't configure any code for Logger at all, I just wrote injection--why would it work? Finally I found out that I was actually using Microsoft's default configuration in the Program class.

The following is part of the source code of CreateDefaultBuilder method. The whole source code is at https://github.com/aspnet/MetaPackages. It can be seen that when creating a project using templates, the console and debug log components are added by default, and the configuration is read from appsettings.json.

Create your own Logging configuration

First modify the Program class

Modify the Startup class as follows. The execution order of this class is Startup constructor> ConfigureServices > Configure.

This structure is clearer.

4. Logging source code analysis The three configurations are actually for injecting log-related services, but the methods invoked are slightly different. Now let's look at the injection process in detail with the second configuration. First call AddLogging method, its implementation source code is as follows:

AddConfiguration is called.

Here's how to print logs:

Let's take a look at the Console's print implementation:

First ConsoleLogger implements the Log method of ILogger and calls WriteMessage method in the method

The following is a list of the most common types of log:

After reading the above, do you know how to use Logging component in. Net Core? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!

Tags: Methods logs configuration source code components code content time more problems projects different at a loss for this function function reason this skills console Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MariaDB Linux Docker Shulou Information