Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Logging logging component in .net Core

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report