Log4NET structure (1) - Logger

xiaoxiao2021-03-06  51

LoggerLogger is the main module for the application and log4net interaction. It is also a module for log4net generates a log of log. Logger is primarily responsible for generating log messages instead of displaying log messages, regarding log messages, the Layout module behind will be involved.

LogGer provides a variety of ways to record any message. You can use multiple Logger instances in your application, and give their maintenance work to the log4net framework as a "named entry". This means that you no longer need to pass the Logger instance generated in the application as a parameter in the application so that other modules of the application can use this instance. The only thing you need to do is to call it through the logger naming.

Currently, log4net manages naming entities in a way similar to .NET namespace. For example, there are two loggers, which are defined as Logger.First.one and Logger.First, then these two are different loggers, and Logger.First is the ancestors of logger.first.one, while logger.first.one inherited Logger.First properties. Logger located at the highest layer of namespace is the default logger, and is also a root logger (ROOT Logger), so the logger is the root logger.

If you want to implement your own logger, log4net provides interface ILOG to implement your own logger. The general structure of ILOG is 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); void Debug (object message, Exception ex); bool isDebugEnabled; bool ISINFOENABLED;

In Logger, log4net provides a class named LogManager to get us to get / create a Logger. LogManager provides method getLogger (), which receives a string type parameter for specifying the name of the Logger:

Log4net.ilog log

=

Log4Net.logmanager.getlogger ("Logger_Name");

LogManager will create one for us when the logger of the specified name does not exist.

Typically, we are all named logger's name as the name of the Logger, so we can use the following method to pass the parameters of getLogger:

System.reflection.methodbase.getCurrentMethod (). DeclaringType

Although the above code is much longer than TypeOf (classname), as the Log4Net helps, the purpose is to copy, paste this code to any class.

Logger Level

As can be seen from the interface of the ILOG, log4net has different records in 5. Why do you need this 5 record mode? In fact, there are different priorities in this 5 record mode. These levels are defined in log4net.spi.Level. You can use any way in your application based on your needs, however, you don't want to waste too much CPU cycle when you use these methods. Therefore, log4net provides 7 levels and their respective Boolean properties to save CPU cycles, as follows: Logger's Different levels: LeveLallow methodboolean propertyValueoffhigHestfatalvoid Fatal (...); Bool isfatalenabled; errorvoid error (...); bool iserrorenable; Warnvoid Warn (...); Bool Iswarnenabled; Infovoid info (...); BOOL ISINFOENABED; Debugvoid Debug (...); Bool IsDebuGenabled; Alllowest

In log4net, each Logger will give a priority in the configuration file, if there is no specified priority, then it will attempt to inherit a priority from its parent class. Similarly, each method of the Logger has a predefined level, as listed in the above table, the info () method has the level of INFO. When runtime, log4net checks the level of the method and the level of profile gives the Logger, and then performs different operations. For example, suppose a logger has an INFO level, then when performing the following: Logger.info ("Message"); Logger.Debug ("Message"); Logger.Warn ("Message"); there will be the following situation : 1, method INFO level is equal to Logger's INFO level, then the INFO method will execute; 2. The level of method debug is lower than the Logger INFO level, then the debug method will not perform or throw an exception; 3, method WARN level high The WARN method will be executed when the Logger's INFO level is. It can be seen that the method will be performed when the level of the method is greater than or equal to the Logger level. Two special levels are also defined in the above table: all and OFF. All means any method can be performed. The OFF is the opposite. In order to clarify those operations, it can be judged by the attributes in the above table:

IF

(logger.IndebugeNable)

{Logger.debug ("message");

转载请注明原文地址:https://www.9cbs.com/read-82351.html

New Post(0)