Logs in. NetCore (2) integrate with the third-party log tool. netcore

Source: Internet
Author: User

Logs in. NetCore (2) integrate with the third-party log tool. netcore
. NetCore logs (2) integrate with third-party log tools

 

0x00 integrate NLog in the. NetCore Logging Component

The previous article discussed the structure of the log framework in. NetCore. This article discussed the integration of third-party log tools in the. NetCore Logging component. You only need to provide the corresponding implementation (ILogger and ILoggerProvider) according to the interface requirements in the Logging component, and then add the implemented ILoggerProvider to LoggerFactory for use, which is very convenient. Here we use NLog as an example for practice. For more information about NLog, see https://github.com/nlog/nlog/wiki. The integration principle is described here.

NLog provides a complete logging framework, which consists of NLog. logger implements logging and routes Logger with different names to different output targets through configuration files or code configuration routing rules (rules.

NLog is integrated into the Logging component. Generally, no excessive configuration is required, but the content of the Log method is forwarded to NLog for processing. Log filtering (routing in NLog) and output format configuration are both configured in NLog. The Logging component only acts as a log Content forwarding function.

NLog Logger must do the following when integrating into the. NetCore Logging component:

1. Pack NLog. Logger as nlogger and implement the ILogger interface.

Pack NLog. Logger as nlogger to implement the ILogger interface. When implementing the Log method, convert the parameters of the Log method in the Logging component to the parameters of the Log method in NLog. Logger, and then call the Log method of NLog. Logger to output logs.

In addition, NLog has its own log level. Therefore, when Logging, you must convert the log level defined in the. NetCore Logging component to the log level in NLog.

2. Implement the ILoggerProvider Interface

 

3. Add the extension method to add ILoggerProvider to LoggerFactory.

One thing to note is that the NLog official example (https://github.com/NLog/NLog.Extensions.Logging) still calls

LogManager.GetCurrentClassLogger();

The method to obtain Logger is unscientific. In actual use, you should still obtain Logger through dependency injection. The Logger is obtained by injecting ILogger <Class> () to the constructor parameters, which is the same as the Name of the Logger obtained by calling the GetCurrentClassLogger () method in the Class, you can normally match the routing rules in the NLog configuration file.

0x01 integrate your own log Tool

After learning about the Logging framework in. NetCore, we can also try to write a logger and integrate it into the Logging component. Next, we will write a tool to record logs in text documents. The following content is only for demonstration, and the actual writing of our own logging tool needs to be more rigorous.

First, you must implement the ILogger interface. The Log method directly outputs the input information to a text file in the format:

Implement the ILoggerProvider interface:

Write an extension method to facilitate addition:

In this way, we can add all the Logger needed in the Startup, which is a mixture of native, third-party, and self-written.

If you are interested in reading the detailed code, you can go:

Https://github.com/durow/NetCoreStudy/tree/master/src/LoggingStudy

0x02 written at the end

The. NetCore Logging component is written here. The design of the Logging component is easy to integrate with third-party log tools. Understanding how the Logging component integrates different Logging tools is not helpful in actual development: (after all, there are so many options, we almost do not need to develop our own logging tools. However, I learned a lot about the software design through studying the Logging components. I also realized the practical application of many design patterns in books that seem to have nothing to find.

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.