Log4net log component

Source: Internet
Author: User

Log4net log component
I. Introduction

Almost all large applications have their own APIs for tracking and debugging. Once a program is deployed, it is unlikely that special debugging tools will be used. However, an administrator may need a powerful log system to diagnose and fix configuration problems. Experience shows that logging is often an important part of the software development cycle. It has the following advantages: it provides a precise environment when the application is running, allowing developers to locate bugs in the application as soon as possible; once the Log output code is added to the program, logs can be generated and output during the program running without manual intervention. In addition, log information can be output to different places (such as the console and files) for future research. Log4net is a log record package designed for this purpose and used in the. NET development environment.

Log4net has four main components: Logger, Repository, Appender, and Layout ).

Ii. Usage

1. Reference The log4net component

2. Implementation Method

Implementation Method 1: 2.1 log is directly implemented on the console without passing through the configuration file

Namespace log4net {class Program {static void Main (string [] args) {// Step 1: Initialize. Here basicaggregator is used to initialize and output to the log4net console by default. config. basicConfigurator. configure (); // Step 2: obtain a logging logger log4net. ILog logger = log4net. logManager. getLogger ("Test"); // Step 3: Appender is required to set the output location. Because basicaggregator is used above, the default output location is used. Therefore, step 4 is not required: set the output format. Here, a default layout (output mode) is provided. // The log output level is Debug level for basicaggregator. You can see all other levels of output logger. info ("hello world"); Console. readKey ();}}}

Method 2: 2.2 Use the configuration file to process logs on the console

<? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <! -- Start log4net configuration --> <! -- Register.. net supports starting with log4net --> <configSections> <section name = "log4net" type = "log4net. config. log4NetConfigurationSectionHandler, log4net "/> </configSections> <! -- Registration ends with support for log4net in. net --> <! -- This node corresponds to the name of the preceding section --> <log4net> <! -- You can set the Append and level of all logstores in a unified manner --> <! -- In this way, you do not need to set every logstore. Each logstore can inherit the configuration information from the root. --> <! -- <Root> <level value = "DEBUG"/> --> <! -- <Appender-ref = "LogFileAppender"/> Configure output to file --> <! -- <Appender-ref = "leleappender"/> </root> --> <! -- You can set the Append and level of all logstores in a unified manner --> <! -- Configure the logstore to start --> <! -- Logger name: namespace. Class name (for your convenience) --> <logger name = "testApp. Logging"> <! -- Set which Appender --> <appender-ref = "leleappender"> </appender-ref> <! -- Set the DEBUG level --> <level value = "DEBUG"/> </logger> <! -- End the logstore configuration --> <! -- Appender configuration output console start --> <! -- You can configure the output location of the Appender. For more information, see <appender name = "leleappender" type = "log4net. Appender. ConsoleAppender"> <! -- Layout used --> <Layout type = "log4net. layout. PatternLayout"> <! -- Output format parameters, every one makes sense --> <param name = "ConversionPattern" value = "% d [% t] %-5 p % c [% x]-% m % n"/> </layout> </appender> <! -- Appender configuration output console ended --> <! -- Appender configuration output to file start --> <appender name = "LogFileAppender" type = "log4net. Appender. FileAppender"> <! -- Output File, also named --> <param name = "File" value = "log-file.txt"/> <! -- Append mode: true --> <param name = "AppendToFile" value = "true"/> <! -- Start output layout --> <Layout type = "log4net. layout. PatternLayout"> <! -- Content Header --> <param name = "Header" value = "[Header] \ r \ n"/> <! -- Content tail --> <param name = "Footer" value = "[Footer] \ r \ n"/> <param name = "ConversionPattern" value = "% d [% t] %-5 p % c [% x]-% m % n "/> </layout> <! -- End of output layout --> </appender> <! -- Appender configuration output to the end of the file --> </log4net> <! -- End log4net configuration --> <startup> <supportedRuntime version = "v4.0" sku = ". NETFramework, Version = v4.5"/> </startup> </configuration>

  

Namespace log4net {class Program {static void Main (string [] args) {// in actual applications, log4net is usually set through the configuration file. config. xmlConfigurator. configure (); // register using xml // testApp. the Logging name corresponds to the xml logger name log4net. ILog logger = log4net. logManager. getLogger ("testApp. logging "); logger. info ("actual production !!! "); Console. ReadKey ();}}}

2.3Web use log4net

First, register log4net in the Application_Start method of the Global. asax file.

Protected void Application_Start (object sender, EventArgs e)
{
Log4net. Config. XmlConfigurator. Configure (); // register with xml
}

When a page error occurs, the Page_Error method is called to record the error.

// Handle page exception records. This event is triggered when an exception occurs on the page.
Protected void Page_Error (object sender, EventArgs e)
{
HttpServerUtility server = this. Server;
Exception ex = server. GetLastError ();
Log4net. ILog logger = log4net. LogManager. GetLogger ("testApp. Logging ");
Logger. Fatal (ex. Message );
}

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.