Log4net configuration instructions

Source: Internet
Author: User

This document was modified on the basis of "greatest truths to simplicity". Thank you again!
(1) Example of the standard log4net configuration document
<? Xmlversion = "1.0" encoding = "UTF-8"?>
<Configuration>
<ConfigSections>
<Sectionname = "log4net"
Type = "log4net. Config. Log4NetConfigurationSectionHandler"/>
</ConfigSections>
<Log4net>
<Root>
<Levelvalue = "WARN"/>
<Appender-refref = "LogFileAppender"/>
<Appender-refref = "leleappender"/>
</Root>
<Loggername = "testApp. Logging">
<Levelvalue = "DEBUG"/>
</Logger>
<Appendername = "LogFileAppender" type = "log4net. Appender. FileAppender">
<Paramname = "File" value = "log-file.txt"/>
<Paramname = "AppendToFile" value = "true"/>
<Layouttype = "log4net. Layout. PatternLayout">
<Paramname = "Header" value = "[Header] & #13; & #10;"/>
<Paramname = "Footer" value = "[Footer] & #13; & #10;"/>
<Paramname = "ConversionPattern" value = "% d [% t] %-5 p % c [% x]-% m % n"/>
</Layout>
<Filtertype = "log4net. Filter. LevelRangeFilter">
<Paramname = "LevelMin" value = "DEBUG"/>
<Paramname = "LevelMax" value = "WARN"/>
</Filter>
</Appender>
<Appendername = "leleappender" type = "log4net. Appender. ConsoleAppender">
<Layouttype = "log4net. Layout. PatternLayout">
<Paramname = "ConversionPattern" value = "% d [% t] %-5 p % c [% x]-% m % n"/>
</Layout>
</Appender>
</Log4net>
</Configuration>
(2) log4net configuration file and program Association
Log4net configurations can be stored in the default configuration file (app. config or web. config) of the application, or in the configuration file specified by yourself. In the log4net framework, log4net. Config. XmlConfigurator is used to define the configuration file at the Assembly level.
(1) In the winform program, if it is configured in the app. config file, the structure of the configuration file is shown in the example. Find the Assemblyinfo. cs file of the current project and add
[Assembly: log4net. Config. XmlConfigurator (ConfigFileExtension = "config", Watch = true)] Keep the default attribute settings for the app. config file.
(2)winformprogram, if the configuration is in the custom file, such as "config.txt. Find the Assemblyinfo. cs file of the current project and add
[Assembly: log4net. Config. XmlConfigurator (ConfigFile = "config.txt", Watch = true)] the attributes of the Config.txt file must be set to copy. Is to output to the output directory.
In this case, the <section> node in the <configSection> node in the sample template can be omitted.

ConfigFile: Specifies the path and file name of our configuration file, including the extension.
ConfigFileExtension: If we use different file extensions for the compiled Assembly, we need to define this property. By default, the assembly configuration file extension is "config ".
Watch (Boolean attribute): This attribute is used by the log4net framework to determine whether to monitor file changes at runtime. If this attribute is true, FileSystemWatcher will be used to monitor file changes, renaming, deletion, and other events.
Where: configfileand configfileextensionextension cannot be used at the same time. configfileindicates the name of the configuration file. For example, configfileextension must be config.txt"
Break ";
You can also use the DOMConfigurator class in the program code to open the configuration file. Class constructor requires a FileInfo object as a parameter to indicate the configuration file name to be opened. This method is the same as setting properties in the Assembly to open a configuration file.
Log4net. Config. DOMConfigurator. Configure (newFileInfo ("TestLogger. Exe. Config "));
The DOMConfigurator class also provides the ConfigureAndWatch (...) method to configure the framework and detect file changes.
(2) node analysis
In the configuration section, define the section node. The node name must be log4net and case sensitive.
<ConfigSections>
<Sectionname = "log4net"
Type = "log4net. Config. Log4NetConfigurationSectionHandler"/>
</ConfigSections>
(3) log4net node description
(1) attributes supported by nodes: debug, update, and threshold
Debug is optional. The value is true or false. The default value is false. Set to true to enable log4net internal debugging.
Update is optional. The value is Merge (Merge) or Overwrite (Overwrite). The default value is Merge. If this parameter is set to Overwrite, the configured database is reset when the configuration is submitted.
Optional. The value is the level registered in the repository (database). The default value is ALL.
(2) Support for Child Elements
Appender 0 or multiple
Logger 0 or multiple
Renderer 0 or multiple
Root can be at most one
Param 0 or multiple
 
(4) root Node description
(1) Root logger, which is inherited by all other logger by default. The root element has no attribute.
(2) supported sub-elements www.2cto.com
Appender-ref: the name of the appender to be referenced.
· The level can be at most one. Only events at this level or above will be recorded.
· Param 0 or multiple. Set some parameters.
<Root>
<Levelvalue = "WARN"/>
<Appender-refref = "LogFileAppender"/>
<Appender-refref = "leleappender"/>
</Root>
(5) Logger node description
(1) supported attributes: name and additi.pdf
Required by name. logger name
Optional. The value is true or false. The default value is true. If it is set to false, the appender in the parent logger will be blocked.
(2) supported child elements:
Appender-ref: the name of the appender to be referenced.
· The level can be at most one. Only events at this level or above will be recorded.
· Param 0 or multiple. Set some parameters.
Example: <loggername = "testApp. Logging">
<Levelvalue = "DEBUG"/>
</Logger>
 
(6) Description of Appender nodes
(1) define the log output mode, which can only be a sub-element of log4net. The name attribute must be unique and the type attribute must be specified. The name and type attributes are supported.
Required by name. name of the Appender object
Required. The output type of the Appender object.
(2) supported child elements:
· Appender-ref: 0 or more. This appender is allowed to reference other appender. This is not supported by all appender types.
· There are 0 or more filters, which define the filters used by this app.
· Layout can be at most one. Define the output format used by the appender.
· Param 0 or multiple. Set the value of the corresponding attribute in the Appender class.
For example, LogFileAppender node log file output. Type = "log4net. Appender. FileAppender"
Leleappender node console output. Type = "log4net. Appender. leleappender"
(7) Filter node description
Filter, which can only be a child element of <appender>.
Supported attributes:
Required. The Filter type.
Supported sub-elements: param 0 or more. Set some parameters.
(8) Layout node description
Layout, which can only be a child element of <appender>.
Supported attributes:
Required. Layout type.
 
Supported sub-elements: param 0 or more. Set some parameters.
(9) Param
<Param> How can an element be a child element.
 
Supported attributes:
Required. The value is the parameter name of the parent object.
(Optional) values and types must have an attribute specified. Value is a string that can be converted into a parameter value.
(Optional) values and types must have an attribute specified. Type is a type name. If type is not defined in the log4net program, you need to use the full name.
Supported sub-elements: param 0 or more. Set some parameters.
 


From mymhj's column

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.