Log4net, c # log4net

Source: Internet
Author: User
Tags log4net

Log4net, c # log4net

The configuration is not completed before, and a demo is inserted in the middle to show how the log is used more intuitively. in this article, I added some of my own things to better understand these configurations.

Here we will continue to introduce the content in the configuration file.

1. <log4net> label

<log4net debug="true"></log4net>

Supported attributes:

Debug Optional. The value is true or false. The default value is false. Set to true to enable log4net internal debugging.
Update Optional. Values: 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.
Threshold Optional. The value is the level registered in the repository (database). The default value is ALL.

Supported elements:

Appender 0 or more
Logger 0 or more
Renderer 0 or more
Root Up to one
Param 0 or more

 

2. <root> label

<root>  <level value="ALL" />  <appender-ref ref="ErrorLogFileAppender" />  <appender-ref ref="TraceLogFileAppender" /></root>

Root element, which is inherited by all other logger by default. root has no attribute.

Supported Child elements:

Appender-ref The name of the appender to be referenced.
Level

Up to one. Only events at this level or above will be recorded.

OFF/FATAL/ERROR/WARN/INFO/DEBUG/ALL

Param 0 or more. Set some parameters.

 

3. <logger> label

In the previous example, the logger tag is not found. If you simply record the log, you do not need to use this tag.

Supported attributes:

Name Required. logger name
Additi.pdf 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.

Supported Child elements:

Appender-ref The name of the appender to be referenced.
Level Up to one. Only events at this level or above will be recorded.
Param 0 or more. Set some parameters.

 

4. <appender> label

<appender name="TraceLogFileAppender" type="log4net.Appender.RollingFileAppender">  <file value="Logs\Trace\" />  <appendToFile value="true" />  <MaxSizeRollBackups value="20" />  <RollingStyle value="Date" />  <DatePattern value="yyyy-MM-dd'.txt'" />  <StaticLogFileName value="false" />  <!--<MaximumFileSize  value="10MB"/>-->  <layout type="log4net.Layout.PatternLayout,log4net">    

Defines 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.

Supported attributes:

Name Required. Name of the Appender object.
Type Required. The output type of the Appender object.

Supported Child elements:

Appender-ref 0 or more. This appender is allowed to reference other appender, which is not supported by all appender types.
Filter 0 or more filters are defined for this app.
Layout Up to one. Define the output format used by the appender.
Param 0 or more. Set the value of the corresponding attribute in the Appender class.

 

In fact, <appender> can contain more than four sub-elements.

In my past projects, there were two common types: Writing files and writing to databases. it has not been used for emails. let's introduce the first two methods.

4.1 writing files

Writing files may be different from each other. Of course, it is also related to projects. some users prefer to store data by date, while others prefer to store data by setting the maximum storage value in a file. yes. if there are many project log files, you can divide them by date. If there are few project log files, the date division will not be calculated, and the number of files and logs may be the same.

1). Date Method

<Appender name = "ErrorLogFileAppender" type = "log4net. Appender. RollingFileAppender"> <! -- File directory or file name --> <file value = "Logs \ Error \"/> <! -- If it is true, only the file name is used for the log file. If it is false, the log file naming rule is file + DatePattern --> <StaticLogFileName value = "false"/> <! -- File name format --> <DatePattern value = "yyyy-MM-dd'.txt '"/> <! -- The log format is Date/Size. Here, logs are recorded by Date --> <RollingStyle value = "Date"/> <! -- Whether to append to log. The default value is true. You do not need to set --> <appendToFile value = "true"/> <layout type = "log4net. layout. patternLayout, log4net "> 

Date method. I have marked it with the background color.

 

2). File size restriction Method

<Appender name = "ErrorLogFileAppender" type = "log4net. appender. rollingFileAppender "> <file value =" Logs \ Error. log "/> <RollingStyle value =" Size "/> <! -- The number of log files recorded in the folder, which must be used with MaximumFileSize --> <MaxSizeRollBackups value = "2"/> <! -- The size of each log file cannot be a decimal number. If the size is limited to kb but the content to be recorded is KB, all logs will be recorded --> <! -- Available units: KB/MB/GB --> <MaximumFileSize value = "100KB"/> <StaticLogFileName value = "true"/> <appendToFile value = "true"/> <layout type = "log4net. layout. patternLayout, log4net "> 

As shown in the preceding configuration, the log files that can be created in folders can be limited. What should I do if they are full? Log4net will not create new files, but will record logs by rewriting. In other words, it turns around these files.

 

4.2 writing data to the database

<? Xml version = "1.0" encoding = "UTF-8"?> <Log4net> <root> <level value = "ALL"/> <appender-ref = "ADONetAppender"/> </root> <appender name = "ADONetAppender" type =" log4net. appender. ADONetAppender "> <! -- Number of cache entries, which are written to the database at a time. During debugging, set this parameter to 1 --> <bufferSize value = "1"/> <param name = "ConnectionType" value = "MySql. data. mySqlClient. mySqlConnection, MySql. data "/> <param name =" ConnectionString "value =" server = localhost; user id = root; password = root; database = current; port = 3306; CharSet = utf8; "/> <commandText value =" insert into ErrorLog (dtDate, sThread, sLevel, sLogger, sMessage, sException) values (@ log_date, @ thread, @ log_level, @ logger, @ message, @ exception); "/> <parameter> <parameterName value =" @ log_date "/> <dbType value =" DateTime "/> <layout type =" log4net. layout. rawTimeStampLayout "/> </parameter> <parameterName value =" @ thread "/> <dbType value =" String "/> <size value =" 100 "/> <layout type = "log4net. layout. patternLayout "> <conversionPattern value =" % t "/> </layout> </parameter> <parameterName value =" @ log_level "/> <dbType value =" String & quot;/> <size value = "200"/> <layout type = "log4net. layout. patternLayout "> <conversionPattern value =" % p "/> </layout> </parameter> <parameterName value =" @ logger "/> <dbType value =" String & quot;/> <size value = "500"/> <layout type = "log4net. layout. patternLayout "> <conversionPattern value =" % logger "/> </layout> </parameter> <parameterName value =" @ message "/> <dbType value =" String & quot;/> <size value = "3000"/> <layout type = "log4net. layout. patternLayout "> <conversionPattern value =" % m "/> </layout> </parameter> <parameterName value =" @ exception "/> <dbType value =" String & quot;/> <size value = "4000"/> <layout type = "log4net. layout. predictionlayout "/> </parameter> </appender> </log4net>

Appendix create table SQL

Create table 'errorlog' ('id' int (11) not null AUTO_INCREMENT, 'dtdate' datetime default null, 'sthread' varchar (100) default null, 'slevel' varchar (200) default null, 'slogger 'varchar (500) default null, 'smessage' varchar (3000) default null, 'sexception' varchar (4000) default null, primary key ('id') ENGINE = InnoDB AUTO_INCREMENT = 21 default charset = utf8 COMMENT = 'error log table'

OK. Now you can view the result directly:

It is easy to configure and use.

 

Refer:

Log4net User Manual

 

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.