. NetCore log (1) log component parsing,. netcore

Source: Internet
Author: User

. NetCore log (1) log component parsing,. netcore
. NetCore log (1) generation of the log component parsing 0x00 Problem

The logging function is often used in development. It can record program running details or user behaviors. During the previous development, I usually used my own gadgets to record logs. The output targets include the console, text files, and databases. Generally, they all create global Logger, call the corresponding Logger to output logs to the target. When there are too many output targets, it may be difficult, but it is acceptable. Start learning. netCore subsequently came into contact with the Logging framework (Logging Component). Although it is completely possible to record logs in the previous way, a more general method should be used to decouple log records from specific output targets. Therefore, I learned the Logging component in. NetCore and tried to implement a custom LoggerProvider, and used the existing third-party Logging Tool NLog IN THE. NetCore Logging framework. Write a blog as a learning record, and also hope to help the garden friends who have this need.

0x01. Logging in NetCore

As described in the previous section, when there are multiple log output targets, writing logs becomes troublesome. Think about it, the log output action remains the same, but it only changes to different output targets (console, text files, databases, etc.), so you can abstract the log record action, the Logger contains multiple output targets. When we call the Log method to write logs, the Log method calls the XxxLogger In the Logger in sequence and writes the logs to the specific target. The process is shown in:

So how can we create such a Logger? We can create a factory named LoggerFactory to produce Logger, And the Logger contains ConsoleLogger and FileLogger. These XxxLogger can be created through XxxLoggerProvider. Furthermore, the behaviors of Logger, LoggerFactory, and LoggerProvider can be abstracted as the ILogger, ILoggerFactory, and ILoggerProvider interfaces.

Where:

The Log () method in ILogger can record logs;

ILoggerProvider can create ILogger to write logs to specific targets;

ILoggerFactory can add multiple iloggerproviders and create the final ILogger;

To use the AddProvider method in LoggerFactory to add ILoggerProvider:

Create a Logger using the CreateLogger method in LoggerFactory:

 

Is the Logger constructor, and uses providers in the passed LoggerFactory to call ILoggerProvider to create XxxLogger.

 

ILoggerFactory and ILoggerProvider both generate ILogger, which seems confusing. However, the implementation details of these two types of ILogger are different. In different implementations, Log () methods have different meanings.

ILoggerFactory generates the Logger type (that is, the Logger we finally use). The Log () method calls the ILogger in the _ loggers array contained in the Logger in sequence.

ILoggerProvider generates different types of XxxLogger (that is, the _ loggers array in the above-mentioned Logger contains such as ConsoleLogger and DebugLogger), and its Log () the method is to write logs to specific targets. The Log () method for ConsoleLogger:

Sometimes we may not want some logs to be written to all targets. For example, you only want to write certain logs to the database. In this case, you can pass in the XxxdLoggerProvider Constructor

Func<string, LogLevel, bool> filter

If true is returned, logs are written. If false is returned, logs are not written.

In addition, different configuration methods are available for different loggerproviders, which are not described here.

0x02 generic Logger <T>

As we can see earlier, Logger uses name to identify its uniqueness. In many cases, we want to record the namespace and type when logs are generated, therefore, using the complete type name as the Logger name ensures uniqueness and records the namespace and type in which logs are generated. When you create a Logger <T> object, you actually create a Logger with the full type name of T as the name and package it, the Logger <T> Log method cannot be passed in to the created Logger Log method.

In this way, name-based routes like NLog are easily integrated.

0x03 use logging

By default, ILoggerFactory has been added to the IServiceCollection container. We only need to add the ILoggerProvider. To make the code more concise and self-explanatory, the Logging component also adds an extension method to ILoggerFactory. For example, you only need to use the following code:

You can add ConsoleLoggerProvider and DebugLoggerProvider.

In addition, the complex Log methods of Logger are encapsulated (LogTrace, LogDebug, LogError, and so on) to meet different requirements.

When using Logger, you can obtain Logger through dependency injection. There are two methods:

And

There is no essential difference between the two methods. As shown in, the CreateLogger <T> method also calls the Logger <T> constructor to create a Logger <T>.

Therefore, you only need to select based on your preferences.

0x04 written at the end

. The Logging component of NetCore provides a Logging framework. All Logging tools that implement the ILoggerProvider interface can be integrated into Logger, which greatly facilitates the integration of mature third-party Logging tools. The Logging component decouples the Logging logic from the specific Logging behavior. You can change the Logging tool without modifying the Logging logic. Similarly, you only need to implement the Framework interface, different logging tools can also be mixed. Therefore, although. NetCore only implements Console, Debug, and other limited Logger, we have many choices with the help of a wide range of third-party logging tools. Even if the requirements are extremely odd, as long as the interface in the framework is implemented, we can easily integrate our own logging tool. In the next article, we will take NLog as an example to describe the integration of third-party logging tools. In addition, we will write and integrate a self-written Logger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.