"Z13 District" "Log4net write the appropriate log class" second article "No Theory of Practice is blind"

Source: Internet
Author: User
Tags ibm db2 apache log log4net

In the first article , the real gun has been done with log4net once. It is also hoped that we can first feel what is log4net. But no theory of practice is certainly blind, so this article,

Let's talk about log4net configuration and other information.

Log4net Source

Log4net is    part of the Apache Software Foundation Apache Logging Services Project. The Apache Log service project is dedicated to providing cross-language logging services for program debugging and auditing.

Detailed description, source code download can refer to official website: http://logging.apache.org/log4net/

Apache Logging Services Engineering Other projects: http://logging.apache.org/

What are log4net capable of?

a     Project no matter how NB, if we do not need, that basic and we have nothing to do. So before you use it, you need to know what kind of work he's capable of.

First, log4net is used to write logs. The main features are as follows:

    • The log4net is optimized for speed.
    • Log4net is based on the named logger hierarchy.
    • Log4net is a failure that stops, but does not depend on laziness.
    • Log4net is thread-safe.
    • Log4net is not limited to a set of pre-defined facilities. Logging behavior can be set at run time using a configuration file. The configuration file is in XML format.
    • Log4net is designed to handle exceptions from the beginning.
    • Log4net can have its output directed to many sinks including: file, console, Windows event log, or e-mail.
    • Log4net classification is classified into levels of logging: DEBUG, INFO, WARN, ERROR and FATAL.
      The format of the log output can easily be changed by implementing a new layout class. By writing a new add-on class can change the log output, as well as the writing strategy objective.
    • The log4net supports multiple output additional destinations per logger.

The main function I have marked the background color, It can also output logs to the database "SQL Server, Access, Oracle, Db2,sqlite"

The above features and log4net log can be output location, I believe in our programming, basically can meet the needs of writing logs.

It also has a feature, that is, the cost of learning is also low. in the first article, we also saw that only one log4net.dll and one copy of the configuration file can be used to output the log directly.

So next, we analyze the specific role of the configuration file.

Log4net structure and configuration file analysis

PS: Configuration file can be customized file name, can also be written in the program's own app.config/web.config.

Main structure:

Log4net has four main components, namely Logger (recorder),Repository (library), Appender (attachment), and layout.

Function Analysis:

 Logger (Logger): is the main component that an application needs to interact with, which is used to generate log messages. The resulting log messages are not displayed directly, and are not output until pre-formatted with layout.

Repository (library): mainly for the maintenance of the log object organization structure.

Appender (Attachment): Defines the output medium. For example: Output to file, console, database, Windows event log, e-mail, and so on.

Layout: Formatting log information. For example: you want to do the log information to increase the current time, log4net have characters instead. [More abstract, look at the following specific operation can]

Configuration file:

<?XML version= "1.0" encoding= "Utf-8"?><Configuration>  <configsections>    < Sectionname= "Log4net"type= "System.Configuration.IgnoreSectionHandler"/>  </configsections>  <log4net>
      <!--the name of the Appender node can be customized, but the ref attribute of the Appender-ref node under the root node must correspond to Apperder name-->
<!--appender nodes and APPERDER-REF nodes should be one by one--
<Appendername= "Consoleappender"type= "log4net." Appender.consoleappender ">        <Layouttype= "log4net." Layout.patternlayout ">          <Conversionpatternvalue= "%date [%thread]%-5level%logger [%PROPERTY{NDC}]-%message%newline" />        </Layout>    </Appender>    <Root>      <!--= = = Control Console control Display log = = -     <Appender-refref= "Consoleappender" />    </Root>  </log4net></Configuration>

The above configuration focuses on the Appender node and the root node. The rest of the nodes are temporarily out of control.

Apperder (Attachment): The Type property determines the exact location of the log output.

Adonetappender: For database output (MS SQL server,ms access,oracle9i,oracle8i,ibm DB2, SQLite) Aspnettraceappender: Aspnettraceappender record message Bufferingforwardingappender: Buffered message, Then output to Apperdercoloredconsoleappender: Send the message to the console, you can set the background, Font Color Consoleappender: Sending a message to the console Eventlogappender: Sending a message to an event in the application Fileappender: writing the log to a file Forwardingappender: detailed crossing network description. Managedcoloredconsoleappender: Can be used to highlight error messages Memoryappender: Memory stream attachment netsendappender: Verbose Crossing network description Outputdebugstringappender: Write log Outp Utdebugstring Apiremotingappender: Pass the log to Remotingrollingfileappender: Fileappender-based, you can set the time or size to overwrite the log. Smtpappender: Send logs via SMTP e-mail smtppickupdirappender: SMTP-like, detailed crossing Web description. Traceappender: Writes the log to the. NET base Class library provided by the tracking System (System.Diagnostics.Trace) Udpappender: Send to Remoteaddress on the specified remoteport. Dynamicpatternlayout: Detailed Crossing network description.
Apperder Type

All of these are my generalizations, some of which are not good generalizations, there is no write, afraid to mislead everyone.

You can also refer to the official website description (with specific configuration information) http://logging.apache.org/log4net/release/config-examples.html

If there is a generalization of better, also want to share with me: z13qu#sina.com

Next, analyze the following special characters:

In the Conversionpattern of Apperder's layout, you can find a lot of characters that you don't quite understand.

"%date [%thread]"
Let's take a concrete look at what they mean:

%M (message): The content of the log (the Consoleloger.info ("This is a line of logs") written in the first article; "This is a line of logs" is%m content)

%n (New line): NewLine

%d (datetime): Outputs the moment at which the current statement runs

%r (Run time): The number of milliseconds that the output program consumes from running to executing to the current statement

%T (thread ID): The thread ID where the current statement is located

%p: The current priority level of the log, which is debug, INFO, WARN ... such as

%c (Class): The name of the current log object

%l: The line number where the output statement is located

%F: The file name where the output statement resides

%-Number: Indicates the minimum length of the item and, if not enough, fills it with a space

Want to look back at the first article of the configuration file and output content, can you see it?
This article has a basic understanding of most theories and configurations. Next, began to use the source code to explain the specific usage process. I will also write most of the comments in the source code.
Hope in the practice operation, feel the most real log4net.

"Z13 District" "Log4net write the appropriate log class" second article "No Theory of Practice is blind"

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.