Log Component parsing

Source: Internet
Author: User

Log component parsing log in Netcore (1) Log component parsing 0x00 problem generation

Logging is commonly used in development to record the details of a program's operation or to record the user's behavior. In the previous development, I usually use their own small tool to record the log, the output target contains the console, text files, databases, generally create a global logger, in the need to log the place to call the corresponding logger output to the corresponding target. Encounter the output target is more sometimes also feel very troublesome, but also can accept. Start learning. Netcore is exposed to the logging framework (logging component), although it is entirely possible to log logs in the previous way, but it should be used in a more general way to decouple log records from specific output targets. So the study. Netcore the logging component, and tries to implement the custom Loggerprovider, as well as in the. The Netcore logging framework uses the existing sophisticated third-party logging tool Nlog. Write a blog as a learning record, but also want to have this aspect of the needs of the park friends are helpful.

0x01. The logging in Netcore

As you can write in the above section, writing logs becomes cumbersome when the target of the log output gets more. Think about it, the log output of this action is unchanged, the change is only a different output target (console, text file, database, etc.), so you can put the log record this action abstraction, the logger contains a number of output targets, when we call the log method to write logs, The log method calls the Xxxlogger in logger and writes the logs to the specific target. The process is as follows:

So how to create such a logger, we can create a factory called loggerfactory used to produce logger,logger in the package also contains Consolelogger, FileLogger, etc. These xxxlogger can be created through Xxxloggerprovider. Further, the behavior of logger, loggerfactory and Loggerprovider can be abstracted as interfaces ILogger, Iloggerfactory, and Iloggerprovider.

which

The log () method in ILogger can record logs;

Iloggerprovider can create ILogger to write logs to a specific target;

Iloggerfactory can add multiple iloggerprovider, and can create the ilogger that we end up using;

To add Iloggerprovider using the AddProvider method in Loggerfactory:

Create logger for loggerfactory using the Createlogger method:

For the logger constructor, use the providers in the incoming loggerfactory, and then call the Iloggerprovider in it to create the Xxxlogger.

It is important to note here that Iloggerfactory and Iloggerprovider both produce ilogger and seem confusing, but actually the implementation details of the two ILogger are different, and the log () method differs in different implementations.

For Iloggerfactory, the logger type is generated (that is, the logger we end up using), and its log () method calls _loggers in the ILogger array contained in logger in turn.

and iloggerprovider produced for all kinds of different xxxlogger (that is, the _loggers array in the above mentioned logger contains such as Consolelogger, Debuglogger), its log () The method is to write the log to the specific target. The log () method for Consolelogger:

At times we may not want some logs to be written to all the targets. For example, just want to write some specific logs to the database. You can then pass in the Xxxdloggerprovider constructor

1 Func<string, LogLevel, bool> filter

The form of a delegate that writes a log when it returns True, and returns false without writing to the log.

In addition, there are different configurations for different loggerprovider, which is not explained here.

Logger<t> of 0x02 Generics

As we saw earlier, logger uses name to identify its uniqueness. In many cases when logging we want to record the namespace and type in which the log was generated, so using the full type name as the name of the logger ensures uniqueness and also records the namespace and the type of the log where it was produced is a good choice. When creating a Logger<t> object, it is actually creating a logger with the full type name of T as name and wrapping it, passing the Logger<t> log method intact into the created logger log method.

This makes it easy to integrate name-based routing like Nlog.

0x03 using log records

Iloggerfactory has been added to the Iservicecollection container by default, we just need to add the required Iloggerprovider. To make the code more concise and self-explanatory, the logging component adds extension methods to iloggerfactory, such as simply using the following code

You can complete the addition of Consoleloggerprovider and Debugloggerprovider.

In addition, the logger complex log method is encapsulated (Logtrace, Logdebug, Logerror, etc.) to meet different requirements.

Logger can be obtained through dependency injection when using logger, which can be obtained in two ways:

And

There is no essential difference between the two methods, as shown in the Createlogger<t> method, which also calls the Logger<t> constructor to create the logger<t>.

So just choose according to your preference.

0x04 written in the last

. The Netcore logging component provides a logging framework that enables the integration of a mature third-party logging tool as long as the logging tool that implements the Iloggerprovider interface can be integrated into the logger. Through the logging component, the logging logic and the specific record behavior is decoupled, you can change the logging tool arbitrarily without modifying the logging logic, as long as the framework interface, different logging tools can be mixed. So though. Netcore itself only implemented a few limited logger, such as console, debug, but with the help of a wealth of third-party logging tools, we have a lot of choices. Even if the demand is extremely wonderful, we can easily integrate our own logging tools as long as we implement the interfaces in the framework. The next article will take Nlog as an example of the integration of third-party logging tools, in addition to writing and integrating a logger of your own writing.

Log Component parsing

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.