Log4net Concise Manual)

Source: Internet
Author: User
Tags log4net
The common problem is not very useful. The reason is that it is too complicated. However, this item appears more and more frequently, so you have to write something for later use. This article only gives a brief description of the use of log4net. All aspects related to expansion and development are ignored.

To use log4net, you must be familiar with logger, appender, and layout. Logger is a logger. We use related methods to record logs. appender is used to set the storage mode and location of logs. One or more appender will be bound to the logger configuration; layout is associated with a specific appender to set the log string format.

1. Logger

All recorders must implement the ilog interface, which provides a large number of methods required for logging. Public interface ilog: iloggerwrapper
{
Void debug (...);
Void error (...);
Void fatal (...);
Void Info (...);
Void warn (...);

Bool isdebugenabled {Get ;}
Bool iserrorenabled {Get ;}
Bool isfatalenabled {Get ;}
Bool isinfoenabled {Get ;}
Bool iswarnenabled {Get ;}
}

Generally, logmanager. getlogger () is used to obtain a logger. Logmanager maintains a hashtable internally, saves the newly created logger reference, and obtains its instance directly from hashtable when needed next time. Ilog log = logmanager. getlogger (this. GetType ());
Log. debug ("aaaaaaaaaaaaaaa ");

All logger parameter settings are directly or indirectly inherited from the root, and the inheritance relationship is similar to namespace. For example, the parameter "mylogger. x.y" is inherited from "mylogger. X ". When we create the "mylooger. x.y" recorder, we will find the recorder setting with this name in the configuration file. If it cannot be found, we will search for the recorder based on the inheritance relationship until the root user is created. Therefore, when creating a logger, we usually use the type name as the recorder name. By default, it uses the root or a parent configuration, but when necessary, we can add a more "Detailed" configuration for the specific type at any time. <? XML version = "1.0" encoding = "UTF-8"?>
<Log4net>
<Appender name = "console" type = "log4net. appender. leleappender">
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% 5 level [% thread] (% file: % line)-% message % newline"/>
</Layout>
</Appender>

<Logger name = "learn. Library. log4nettest">
<Level value = "all"/>
</Logger>

<Root>
<Level value = "off"/>
<Appender-ref = "console"/>
</Root>
</Log4net>

When creating logger settings, pay attention to the "level" parameter. Log4net allows us to adjust the log record level through this parameter. Only logs above or equal to this level will be recorded. For example, in the Code debugging phase, we may want to record all the information, but in the deployment phase, we only want to record higher-level error information. This parameter allows us to adjust the record level at any time without modifying the code. (High) off> fatal> error> warn> info> debug> All (low)

The "appender-Ref" parameter is used to bind one or more specific appender. <? XML version = "1.0" encoding = "UTF-8"?>
<Log4net>
<Appender name = "console" type = "log4net. appender. leleappender">
</Appender>

<Appender name = "rollingfile" type = "log4net. appender. rollingfileappender">
</Appender>

<Root>
<Level value = "debug"/>
<Appender-ref = "console"/>
<Appender-ref = "rollingfile"/>
</Root>
</Log4net>

2. appender/Layout

Log4net provides a large number of appender, including adonetappender, aspnettraceappender, consoleappender, fileappender, and outputdebugstringappender. Each appender has specific parameters. When using the appender, you can directly copy it from the example in the log4net manual. (The code is taken from the log4net manual)

(1) aspnettraceappender<Appender name = "aspnettraceappender" type = "log4net. appender. aspnettraceappender">
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% date [% thread] %-5 level % logger [% property {NDC}]-% message % newline"/>
</Layout>
</Appender>

(2) leleappender<Appender name = "leleappender" type = "log4net. appender. consoleappender">
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% date [% thread] %-5 level % logger [% property {NDC}]-% message % newline"/>
</Layout>
</Appender>

(3) outputdebugstringappender<Appender name = "outputdebugstringappender" type = "log4net. appender. outputdebugstringappender">
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% date [% thread] %-5 level % logger [% property {NDC}]-% message % newline"/>
</Layout>
</Appender>

(4) fileappender<Appender name = "fileappender" type = "log4net. appender. fileappender">
<File value = "log-file.txt"/>
<Appendtofile value = "true"/>
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% date [% thread] %-5 level % logger [% property {NDC}]-% message % newline"/>
</Layout>
</Appender>

For more information about layout, see related log4net documents.

3. Configuration

Log4net supports flexible configuration, that is, it can be written to the application configuration file or an independent configuration file. It also provides the ability to monitor configuration file changes, so that we can adjust the configuration at any time without restarting the application.

(1) Use app. config/Web. config

App. config/Web. config

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
<Section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net"/>
</Configsections>

<Log4net>
<Appender name = "console" type = "log4net. appender. leleappender">
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% 5 level [% thread] (% file: % line)-% message % newline"/>
</Layout>
</Appender>

<Root>
<Level value = "debug"/>
<Appender-ref = "console"/>
</Root>
</Log4net>
</Configuration>

Use code to initialize the configuration.

Log4net. config. xmlconfigurator. Configure ();

(2) Use a custom configuration file

Test. log4net

<? XML version = "1.0" encoding = "UTF-8"?>
<Log4net>
<Appender name = "console" type = "log4net. appender. leleappender">
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% 5 level [% thread] (% file: % line)-% message % newline"/>
</Layout>
</Appender>

<Root>
<Level value = "debug"/>
<Appender-ref = "console"/>
</Root>
</Log4net>

Use code to initialize the configuration.

Log4net. config. xmlconfigurator. Configure (New fileinfo ("test. log4net "));

In addition to initializing the configuration, the xmlconfigurator. configureandwatch () method also monitors the configuration file changes. Once modified, the configuration is automatically refreshed.

(3) xmlconfiguratorattribute

We can also use xmlconfiguratorattribute to replace xmlconfigurator. config ()/configureandwatch (). configuratorattribute is used to define the configuration file name associated with assembly.

Method 1: Associate with test. log4net and monitor changes.

[Assembly: log4net. config. xmlconfigurator (configfile = "test. log4net", watch = true)]

Method 2: Associate with test.exe. log4net (or test. dll. log4net, with the prefix of the file name as the current Assembly name) and monitor changes.

[Assembly: log4net. config. xmlconfigurator (configfileextension = "log4net", watch = true)]

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.