How to use ASP. NET to debug APIs

Source: Internet
Author: User
Tags log4net

The following is the API debugging guide for ASP. NET: Log4net.

1 Overview

1.1 The Advantages of Log4net:

Almost all large applications have their own APIs for tracking and debugging. Once a program is deployed, it is unlikely that special debugging tools will be used. However, an administrator may need a powerful log system to diagnose and fix configuration problems.

Experience shows that logging is often an important part of the software development cycle. It has the following advantages: it provides a precise environment when the application is running, allowing developers to locate bugs in the application as soon as possible; once the Log output code is added to the program, logs can be generated and output during the program running without manual intervention. In addition, log information can be output to different local consoles, files, etc.) for future research.

Log4net is a log record package designed for this purpose and used in the. NET development environment.

1.2 install the ASP. NET debugging tool Log4net:

You can download the source code of log4netfrom http://logging.apache.org/log4net. After the package is decompressed, load log4net. sln to Visual Studio. NET in the decompressed src directory. After compilation, you can obtain log4net. dll. To add the log function to your program, you only need to introduce log4net. dll into the project.

2 Log4net Structure

Log4net has four main components: Logger recorder), Repository Library), Appender attachment) and Layout ).

2.1 Logger

2.1.1 Logger interface

Logger is the main component for applications to interact and is used to generate log messages. The generated log messages are not directly displayed. They must be formatted by Layout before being output.

Logger provides multiple methods to record a log message. You can create multiple Logger in your application. Each instantiated Logger object is named as a nameentity (named entity) by the log4net framework). This means that in order to reuse a Logger object, you do not have to pass it between different classes or objects. You just need to call it with its name as a parameter. The log4net framework uses an inheritance system, which is similar to the namespace in. NET. That is to say, if there are two logger defined as a. B. c and a. B, then we say a. B is the ancestor of a. B. c. Each logger inherits the attributes of the ancestor.

The Log4net Framework defines an ILog interface, which must be implemented by all logger classes. If you want to implement a custom logger, you must first implement this interface. You can refer to several examples in the/extension directory.

The ILog interface is defined as follows:

 
 
  1. Public InterfaceILog
  2. {
  3. VoidDebug (ObjectMessage );
  4. VoidInfo (ObjectMessage );
  5. VoidWarn (ObjectMessage );
  6. VoidError (ObjectMessage );
  7. VoidFatal (ObjectMessage );
  8. // Each of the preceding methods has an overloaded method to support exception handling. 
  9. // Every overload method is shown as follows, with an additional parameter of the exception type. 
  10. VoidDebug (ObjectMessage, Exception ex );
  11. //... 
  12. // Boolean attribute is used to check the log level of Logger 
  13. // We will see the log level soon) 
  14. BoolIsDebugEnabled;
  15. BoolIsInfoEnabled;
  16. //... Boolean attribute corresponding to other methods 
  17. }

The Log4net Framework defines a class called LogManager to manage all logger objects. It has a static GetLogger () method that uses the name parameters we provide to retrieve existing Logger objects. If this Logger object does not exist in the framework, it will also create a Logger object for us. The Code is as follows:

Log4net. ILog log = log4net. LogManager. GetLogger ("logger-name ");

Generally, we call GetLogger () with the type of class) as the parameter to track the classes that we are logging. The passed class type can be obtained using the typeof (Classname) method, or the following reflection method can be used:

System. Reflection. MethodBase. GetCurrentMethod (). DeclaringType

Although the symbol is longer, the latter can be used for some occasions, such as obtaining the type of the class that calls the method ).

2.1.2 Log Level

As you can see in the ILog interface, there are five different ways to track an application. In fact, these five methods work on different log priority levels set by the Logger object. These different levels are defined as constants in the log4net. spi. Level class. You can use any method in the program. However, in the final release, you may not want to waste all the code on your CPU cycle. Therefore, the Framework provides seven levels and corresponding Boolean attributes to control the log record type.

Level has the following values:

 

Table 1 Logger Log Level

In the log4net framework, by setting the configuration file, each log object is assigned a log priority level. If a log object is not explicitly assigned a level, the object tries to inherit a level value from its ancestor.

Each method of the ILog interface has a pre-defined level value. As you can see in Table 1, the ILog Inof () method has the INFO level. Similarly, the Error () method has the ERROR level. When we use any of the preceding methods, the log4net framework checks the logger level and method level of the log object. Log requests are accepted and executed only when the method level is higher than the log level.

For example, when you create a log object and set its level to INFO. Therefore, the Framework sets each Boolean attribute of the log. When you call a log method, the Framework checks the corresponding Boolean attribute to determine whether the method can be executed. The following code:

 
 
  1. Logger.Info("message");   
  2. Logger.Debug("message");   
  3. Logger.Warn("message");  

For the first method, the Info () level and the log level INFO), so the log request will be passed and we can get the output result "message ".

For the second method, the Debug () level is lower than the log level (INFO) of the log object logger. Therefore, the log request is rejected and we cannot get any output. Similarly, we can easily draw conclusions for the third line of statements.

Table 1 has two special levels: ALL and OFF. ALL indicates that ALL log requests are allowed. OFF is used to reject all requests.

You can also explicitly check the Boolean attribute of the Logger object, as shown below:

 
 
  1. if (logger.IsDebugEnabled)   
  2. {   
  3. Logger.Debug("message");   
  4. }  

2.2 Repository

Repository is mainly used to maintain the organizational structure of log objects. In earlier versions of log4net, the framework only supports hierarchical organization ). This hierarchical structure is essentially an implementation of the database and is defined in the log4net. Repository. Hierarchy namespace. To implement a Repository, you must implement the log4net. Repository. ILoggerRepository interface. However, this interface is not directly implemented, but inherited based on the log4net. Repository. LoggerRepositorySkeleton class. Hierarchical repository is implemented by the log4net. Repository. Hierarchy. Hierarchy class.

If you are a log4net framework user, rather than an extension, you will hardly use the Repository class in your code. Instead, you need to use the LogManager class to automatically manage databases and log objects.

2.3 Appender

A good log framework should be able to generate multi-destination output. For example, output to the console or save to a log file. Log4net can meet these requirements. It uses a component called Appender to define the output media. As shown in the name, these components attach them to the Logger log component and pass the output to the output stream. You can attach multiple Appender components to a log object. The Log4net framework provides several Appender components. The complete list of Appender components provided by log4net can be found in the log4net framework help manual. With these ready-made Appender components, you generally do not have to write them yourself. But if you want to, you can inherit from the log4net. Appender. AppenderSkeleton class.

2.4 Appender Filters

By default, an Appender object transmits all log events to the output stream. Appender Filters can filter log events according to different standards. There are several predefined filters in the log4net. Filter namespace. With these filters, you can filter log events by log level range or by a special string. You can find it in the Help file of the API

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.