Log4Net Introduction

Disclaimer: This article belongs to the author and blog Park total, welcome to reprint, but without the author's consent declared by this section must be retained, and given the original connection in the apparent position of the article page, otherwise the right to pursue legal responsibilities. https://blog.csdn.net/mymhj/article/details/7516963

            log4net is a logging feature-known open source components. Log .net application can be recorded by it into a variety of media, including: file, console, window transaction log and database (MS SQL Server, Access, Oracle9i, DB2, SQLite), and we can also setting and denoting a level such as logs.

            And, like most other open source components, log4net also came from excessive Java project. log4net from java application log4J.

           log4net's official website: http://logging.apache.org/

           Download the latest version: http://logging.apache.org/log4net/download_log4net.cgi   latest version is: 1.2.11

(A) characteristics:

       (1) open, the source code to be visible.

       (2) more compatible with .net Framework (.Net1.0,2.0,3.0,3.5,4.0)

       (3) support a variety of output log information and log information customized format

        (4) support xml dynamic configuration and configuration management process for the entire log

        (5) modular design and scalability (interface)

         (6) having a high flexibility and performance

(B) the core components

     log4net There are four core components:

         (1) logger, recorder

         (2) Appender, is attached

         (3) Layout, layout

         (4) Repository (library),

Level (c) log

    log4net offers seven log level for us, the priority from high in the end the following table:

level

AllowedMethod

Boolean properties

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

       In log4net framework, by setting the configuration files, each log object is assigned a priority level logging. If you do not give a log object to explicitly assign a level, then the object will attempt to inherit a level value from his ancestors. (Arranged inside the root node)

       An example, when you create a log object, and his level is set to INFO. Thus each frame sets the Boolean property log. When you call the appropriate logging methods, the framework will check the appropriate Boolean property to determine the method can not be performed. The following code:

Logger.Info("message");

Logger.Debug("message");

Logger.Warn("message");

For the first method, Info () level with the logging level and the like (the INFO), thus log request is passed, we can obtain the output "message".

For the second method, the Debug () level is lower than the log level logger log object (the INFO), thus log the request is rejected, and we can not get any output. Similarly, for the third line statement, we can easily conclude.

There are two special levels in Table 1: ALL and OFF. ALL representation allows all log requests. OFF reject all requests.

You can explicitly checked Logger Boolean property of an object, as follows:

if (logger.IsDebugEnabled)

{

  Logger.Debug("message");

}

(D) logger

    Logger is a key component applications need to interact, which is used to log messages. Generating a log message is not directly displayed, but also after pre-treatment will be formatted Layout output.

This part, and also interact directly with the user code in the sub-layer, conventional usage as follows:

Log4net called framework defines LogManager class logger is used to manage all objects. It has a GetLogger () static method, using the name of the parameter that we provide to retrieve the Logger object that already exists. If the Logger object framework does not exist, it will create a Logger object for us. Code as follows:

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

Generally speaking, we will class (class) type (type) as a parameter to call GetLogger (), in order to track our ongoing logging class. Transmission class (class) of the type (type) may be used typeof (Classname) method to get, or can use the following methods to obtain reflection:

System.Reflection.MethodBase.GetCurrentMethod().DeclaringType

Although some symbol length, but the latter may be used for some applications, such as access class (class) method call type (type).

 

(E) Appender

        A good logging framework should be able to generate multiple output destinations. For example, output to the console or saved to a log file. log4net be able to satisfy these requirements. It uses a component named Appender to define the output medium. As the name implies, these components are attached to them and outputs Logger logging component to the output stream. You can put more Appender components attached to a log object. Log4net Appender framework provides several components. For a complete list of components Appender log4net provides help manual can be found in log4net framework. With these ready-made components Appender, you generally no need to have written. But if you prefer, you can inherit from log4net.Appender.AppenderSkeleton class.

(F) Layout

        Layout component for displaying output to the user through the final formatted information. Output information may be displayed in a variety of formats, mainly dependent on the type of component Layout we use. It can be linear or an XML file. Layout component and work with a Appender components. API help book there is a list of the different Layout components. Appender a subject, only correspond to a Layout object. To implement your own Layout class, you need to inherit from log4net.Layout.LayoutSkeleton class that implements the ILayout interface.

(七)Repository

       Repository is mainly responsible for the maintenance log object organizational structure. In previous versions of log4net, the framework supports only hierarchical organizational structure (hierarchical organization). It is a library that implements the structural nature of this level, and is defined in log4net.Repository.Hierarchy namespace. To achieve a Repository, the need to achieve log4net.Repository.ILoggerRepository interface. But usually not directly implement this interface, but in log4net.Repository.LoggerRepositorySkeleton base class inheritance. Library system (hierarchical repository) by log4net.Repository.Hierarchy.Hierarchy class implementation.

If you are a user log4net framework, rather than extend those, then you almost never use the Repository classes in your code. Instead, you need to use LogManager class to automatically manage and logging objects.

 

The details of a configuration file using log4net and configuring each node behind.

 

Guess you like

Origin blog.csdn.net/mymhj/article/details/7516963