Log4NET User Manual

Source: Internet
Author: User
Tags log4net

Disclaimer: This article is mainly translated from Nauman Leghari's Using log4net
Download the sample code here
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 places (such as the console and files) for future research.
Log4net is a log record package designed for this purpose and used in the. NET development environment.
1.2 installation of 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, Repository, Appender, 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:
Public interface ILog
{
Void Debug (object message );
Void Info (object message );
Void Warn (object message );
Void Error (object message );
Void Fatal (object message );

// Each of the preceding methods has an overloaded method to support exception handling.
// Every overload method is shown as follows, with an additional parameter of the exception type.
Void Debug (object message, Exception ex );
//...

// Boolean attribute is used to check the log level of Logger
// (We will see the log level later)
Bool isDebugEnabled;
Bool isInfoEnabled;
//... Boolean attribute corresponding to other methods
}
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 the 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:
Level
Allowed methods
Boolean attribute
Priority
OFF


Highest
FATAL
Void Fatal (...);
Bool IsFatalEnabled;

RROR
Void Error (...);
Bool IsErrorEnabled;

WARN
Void Warn (...);
Bool IsWarnEnabled;

INFO
Void Info (...);
Bool IsInfoEnabled;

DEBUG
Void Debug (...);
Bool IsDebugEnabled;

ALL


Lowest
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:

Logger. Info ("message ");
Logger. Debug ("message ");
Logger. Warn ("message ");
For the first method, the level of Info () 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:
If (logger. IsDebugEnabled)
{
Logger. Debug ("message ");
}

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.