Description of Apache log4net configuration, apachelog4net

Source: Internet
Author: User
Tags log4net

Description of Apache log4net configuration, apachelog4net

Log4net can actually be used*. Config configuration file that comes with the SystemYou can also useOther configuration files.

The two configuration methods determine the methods used by log4net to read the configuration information, which also determines some features.

Log4net can be used to read configuration files in two ways: ASP. NETSystem. Configuration API, The other is log4netLog4net. Config. XmlConfigurator.


So where are their differences?

System. Configuration API:

1. Only the *. config configuration file that comes with the system can be used for configuration (WinForm is XXX.exe. config and WebForm is web. config ).

(Because System. configuration API cannot re-load the Configuration file. This is a literal translation of the English document. I guess it means that the Configuration file can only come from the system *. the config configuration file can be read at one time and cannot be read twice. Therefore, other configuration files cannot be used for configuration .)

2. configuration file changes cannot be monitored. It means that once your program is running, it does not work if you modify the configuration file (for example, from writing a text file to writing a database.

(This can be seen from the above explanation, because the System. Configuration API cannot re-load the Configuration file .)

3. Read configuration permissions follow ASP. NET or IIS. Therefore, when configuring application permissions, you do not have to worry about the permissions of the configuration file.


Log4net. Config. XmlConfigurator:

1. In contrast to the above, you can use the *. config configuration file that comes with the system for configuration, or other configuration files for configuration.

2. In contrast to the above, you can perform monitoring and any changes will take effect immediately.

3. On the contrary, check whether the program has the permission to read files.

How should we configure the two methods?

System. Configuration API:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <configuration> 3 <configSections> 4 <section name = "log4net" type = "log4net. Config. Log4NetConfigurationSectionHandler, log4net"/> 5 <! -- This section is very important. It means that the following log4net section uses the program to process it. This program actually calls System. configuration API. configuration API. --> 6 </configSections> 7 <log4net> 8 <! -- Here are some specific configurations --> 9 </log4net> 10 </configuration>Web. config 1 [XmlConfigurator] 2 // [XmlConfigurator (ConfigFile = "xxx. config")] // unavailable. 3 // [XmlConfigurator (ConfigFileExtension = "config")] // unavailable. 4 // [XmlConfigurator (Watch = true)] // unavailable. 5 class Program 6 {7 static void Main (string [] args) 8 {9 // The following two are available, either. 10 XmlConfigurator. Configure (); // This is equivalent to [XmlConfigurator]. 11 XmlConfigurator. Configure (log4net. LogManager. GetRepository ("DEBUG"); 12 13 // The following is unavailable. 14 // XmlConfigurator. ConfigureAndWatch (new System. IO. FileInfo (@"~ \ Xxx. config "); // unavailable. 15} 16}Log4netDemo. cs


Log4net. Config. XmlConfigurator:

1. Use other configuration files for configuration.

If other configuration files are used for configuration *. you do not need to add anything to the config file. directly create a configuration file (for example, xxx. config). You only need to have the log4net section.

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

2. Use the *. config configuration file that comes with the system to configure.

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <configuration> 3 <configSections> 4 <section name = "log4net" type = "System. Configuration. IgnoreSectionHandler"/> 5 <! -- Because this is the system configuration file, every section in it must be correct, but the following log4net is incorrect for the system. If this section is not written, an error will be reported, this section indicates ignoring the section log4net. --> 6 </configSections> 7 <log4net> 8 <! -- Here are some specific configurations --> 9 </log4net> 10 </configuration>Web. config 1 // all of the following are available. 2 [XmlConfigurator] // use the *. config configuration file that comes with the system. 3 [XmlConfigurator (ConfigFile = "xxx. config")] // use other configuration files for configuration. 4 [XmlConfigurator (ConfigFileExtension = "config")] // use other configuration files for configuration. 5 [XmlConfigurator (Watch = true)] // monitoring configuration file. 6 class Program 7 {8 static void Main (string [] args) 9 {10 // all of the following are available. 11 XmlConfigurator. Configure (); // This is equivalent to the [XmlConfigurator] statement. 12 XmlConfigurator. Configure (log4net. LogManager. GetRepository ("DEBUG"); 13 14 XmlConfigurator. ConfigureAndWatch (new System. IO. FileInfo (@"~ \ Xxx. config "); // This is equivalent to the [XmlConfigurator (ConfigFile =" xxx. config ", Watch = true)] Declaration. 15} 16}Log4netDemo. cs


The above code is just a demonstration, and the syntax in it is wrong. After fully understanding it, we should configure it with our own project.


How do I configure to output logs to different files every day?

<Param name = "DatePattern" value = "yyyy-MM-dd.HH". log "/>
For details, see blog.csdn.net/...669232.

How does log4net put the configuration file in a program?

It can be compiled into a dll or exe as a resource file and released when used. However, you can also write code to replace log4net.
 

Related Article

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.