Partial explanation of Apache log4net configuration

Source: Internet
Author: User
Tags log4net

Log4net can actually be configured using the system's own *.config configuration file , or it can be configured using a different configuration file .

These two configurations determine what log4net uses to read configuration information, which in turn determines some features.

There are two ways that log4net can be used to read configuration files. one is the ASP. system.configuration API, the other is the log4net of Log4net . Config.xmlconfigurator.


So what's the difference between them?

System.Configuration API:

1, can only use the system's own *.config configuration file to configure (WinForm is Xxx.exe.config,webform is the Web. config).

("Because the system.configuration API can not reload the configuration file", this is a literal translation of English documents, I guess he means that the configuration file can only be read from the system's own *.config configuration file one time, not two reads, Therefore, you cannot use other configuration files to configure, even if the configuration can not read. )

2, can not monitor the configuration file changes. This means that once your program is running, it is not useful to modify the configuration file (for example, to write to a database by writing to a text file).

(This can be explained from the above explanation, because the System.Configuration API cannot reload the configuration file.) )

3, read configuration permissions follow the ASP. NET or IIS, so when you configure application permissions, you do not have to worry about the permissions of the configuration file at all.


Log4net. Config.xmlconfigurator:

1, in contrast to the above, you can use the system comes with the *.config configuration file to configure, you can also use other configuration files to configure.

2, contrary to the above, can be monitored, there are changes to take effect immediately.

3, contrary to the above, it is absolutely necessary to understand whether the program has permission to read files.

How should the two methods be configured?

System.Configuration API:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Configuration>3     <configsections>4         < Sectionname= "Log4net"type= "log4net." Config.log4netconfigurationsectionhandler, log4net " />5         <!--This paragraph is very important, it means that the following log4net this section using the program to deal with, this program is actually called System.Configuration API, so the declaration of this paragraph is to represent the use of System.Configuration API.  -6     </configsections>7     <log4net>8         <!--here are some specific configurations -9     </log4net>Ten </Configuration>
Web. config
1 [Xmlconfigurator]2 //[Xmlconfigurator (configfile = "Xxx.config")]//not available. 3 //[Xmlconfigurator (configfileextension = "config")]//not available. 4 //[Xmlconfigurator (Watch = true)]//not available. 5 class Program6 {7     Static voidMain (string[] args)8     {9         //The following two are available, two optional one. TenXmlconfigurator.configure ();//This article is equivalent to [Xmlconfigurator].  OneXmlconfigurator.configure (log4net. Logmanager.getrepository ("DEBUG")); A  -         //The following is not available.  -         //Xmlconfigurator.configureandwatch (New System.IO.FileInfo (@ "~\xxx.config"));//not available.  the     } -}
Log4netDemo.cs


Log4net. Config.xmlconfigurator:

1. Use other configuration file configuration.

If you use a different configuration file to configure, then the system comes with the *.config configuration file without adding anything, directly create a new configuration file (such as: Xxx.config), as long as there is log4net this section can be.

1 <? XML version= "1.0" encoding= "Utf-8" ?> 2 < log4net > 3     <!-- here are some specific configurations  - 4 </ log4net >
Xxx.config

2, using the system's own *.config configuration file configuration.

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Configuration>3     <configsections>4         < Sectionname= "Log4net"type= "System.Configuration.IgnoreSectionHandler" />5         <!--because this is the system configuration file, so the inside of each section should be correct, but the following log4net for the system is not correct, if not write this paragraph will be an error, this paragraph means to ignore the meaning of log4net this section.  -6     </configsections>7     <log4net>8         <!--here are some specific configurations -9     </log4net>Ten </Configuration>
Web. config
1 //the following are available. 2[Xmlconfigurator]//Use the *.config configuration file that comes with your system. 3[Xmlconfigurator (ConfigFile ="Xxx.config")]//use a different configuration file configuration. 4[Xmlconfigurator (configfileextension ="Config")]//use a different configuration file configuration. 5[Xmlconfigurator (Watch =true)]//monitoring configuration files. 6 class Program7 {8     Static voidMain (string[] args)9     {Ten         //the following are available.  OneXmlconfigurator.configure ();//This article is equivalent to the [xmlconfigurator] declaration.  AXmlconfigurator.configure (log4net. Logmanager.getrepository ("DEBUG")); -  -Xmlconfigurator.configureandwatch (NewSystem.IO.FileInfo (@"~\xxx.config"));//this is equivalent to [xmlconfigurator (configfile = "Xxx.config", Watch = True)] declaration.  the     } -}
Log4netDemo.cs


The above code is just a display, the syntax is wrong, should be fully understood, combined with their own projects to match.

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.