1. Introduction
Logger is one of the three core carriers of Log4net, and it is of great significance to make it clear. The other two were Appender and Layout. The corresponding relationship is one logger corresponding to multiple appender, and one appender corresponds to one layout.
For the internal implementation of Log4net, ILogger is the interface (all logs need to be implemented--interface that all loggers implement).
Why not the ILog interface we use? In fact, Ilog is the ilogger of the packaging, is a typical wrapper mode, words do not look carefully, it is not clear their relationship.
Indicates the relationship: Ilog-->iloggerwrapper-->ilogger.
So, starting from below, please ignore Ilog interface, focus on ILogger, this is the focus of this article.
2.ILogger interface
Method |
Meaning |
String name{get;} |
Gets the name of the ILogger |
Log (Type Callerstackboundarydeclaringtype, level level, object message, Exception Exception); |
Turns the information that needs to be logged (level, MSG, ex) into a Loggerevent object, and then distributes the Loggerevent object to each appender. |
void Log (Loggingevent logEvent); |
Record a loggerevent directly |
BOOL Isenabledfor (level level); |
Check if a level is available |
Iloggerrepository Repository {get;} |
The warehouse where ILogger is located |
From the interface with the source code can be drawn:
2.1 Warehousing
Through the Repository can find ILogger where the warehouse, get the corresponding group (Repository group);
Design Ideas :
Multiple log objects are grouped for ease of administration. For example users user, user group UserGroup, where UserGroup is a grouping.
Repository, in addition to the meaning of grouping, has the function of storage, which can be understood as a factory with caching function (Factory+cache).
2.2 Loggerevent
What is loggerevent again? (Long time ago to see loggerevent, have been very tangled why this name?) Is it to make others not understand the source code? After careful examination, such as the request in WCF's MESSAGING,EBAYSDK, is just a feature title in a context)
Design Ideas :
Loggerevent can be understood as an envelope envelope, similar to the concept of soap in WebService, and similar to the messaging design in MessageQueue. It is primarily used to wrap data about messages (objectmessage). This enables the output to multiple terminals by distributing the loggerevent to the Appender. The following code:
- Virtual protected void Callappenders (loggingevent loggingevent)
- {
- //......
- int writes = 0;
- for (Logger C = this; c! = null; c = c.parent)
- {
- if (C.m_appenderattachedimpl! = null)
- {
- //Protected against simultaneous call to Addappender, Removeappender,...
- C.m_appenderlock.acquirereaderlock ();
- Try
- {
- if (C.m_appenderattachedimpl! = null)
- {
- Writes + = c.M_appenderattachedimpl. Appendlooponappenders (loggingevent);
- }
- }
- finally
- {
- C.m_appenderlock.releasereaderlock ();
- }
- }
- //......
- }
- }
Where M_appenderattachedimpl is a collection of Appender, the appendlooponappenders Loop adds loggerevent to Appender.
2.3 Loggereventdata
What is the data in "envelopes"? The--loggereventdata object.
- Public struct Loggingeventdata
- {
- Public string Loggername;
- Public Level level;
- Public string Message;
- Public string ThreadName;
- Public DateTime TimeStamp;
- Public Locationinfo Locationinfo;
- Public string UserName;
- Public string Identity;
- Public string exceptionstring;
- Public string Domain;
- Public Propertiesdictionary Properties;
- }
The most commonly used data, timestamp,message,exceptionstring
3. Abstract class Logger
Characteristics:
1) by inheriting the Iappenderattachable interface, so that there is a one-to-many inclusion and invocation relationship between logger and iappender;
2) Logger has a hierarchical structure, published the type of logger father pointer parent;
3) by implementing the ILogger (specifically using the Loggerevent object), the whole of the log is packaged up;
4.ILoggerFactory
Used to create Logger objects
Its implementation in Defaultloggerfactory is:
- public Logger Createlogger ( Iloggerrepository Repository, string name)
- {
- if (name = = Span style= "color:blue;" >null )
- {
- return new Rootlogger (repository. Levelmap.lookupwithdefault (Level.debug));
-    
- return new Loggerimpl (name);
- }
among them, the Loggerimpl object inherits from the logger, and does not make any special changes.
The Rootlogger comes with a default level.
Summarize:
Logger actually provides a processing center function, like a distribution station, to send a message to each appender.
To be Continued ~
Logger analysis of Log4net Source view