Logger analysis of Log4net Source view

Source: Internet
Author: User
Tags log4net

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:

  1. Virtual protected void Callappenders (loggingevent loggingevent)
  2. {
  3. //......
  4. int writes = 0;
  5. for (Logger C = this; c! = null; c = c.parent)
  6. {
  7. if (C.m_appenderattachedimpl! = null)
  8. {
  9. //Protected against simultaneous call to Addappender, Removeappender,...
  10. C.m_appenderlock.acquirereaderlock ();
  11. Try
  12. {
  13. if (C.m_appenderattachedimpl! = null)
  14. {
  15. Writes + = c.M_appenderattachedimpl. Appendlooponappenders (loggingevent);
  16. }
  17. }
  18. finally
  19. {
  20. C.m_appenderlock.releasereaderlock ();
  21. }
  22. }
  23. //......
  24. }
  25. }

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.

  1. Public struct Loggingeventdata
  2. {
  3. Public string Loggername;
  4. Public Level level;
  5. Public string Message;
  6. Public string ThreadName;
  7. Public DateTime TimeStamp;
  8. Public Locationinfo Locationinfo;
  9. Public string UserName;
  10. Public string Identity;
  11. Public string exceptionstring;
  12. Public string Domain;
  13. Public Propertiesdictionary Properties;
  14. }

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:

    1. public Logger Createlogger ( Iloggerrepository Repository, string name)
    2. {
    3. if (name = = Span style= "color:blue;" >null )
    4.    {
    5.       return new Rootlogger (repository. Levelmap.lookupwithdefault (Level.debug));
    6.    
    7.    return new Loggerimpl (name);
    8. }

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

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.