Troubleshooting Log4net exclusive log files and various output configurations for log4net (Appender)

Source: Internet
Author: User
Tags documentation log4net
Because the log4net is exclusive to the log file by default, the file cannot be file.open.
Can be increased by configuring:< LockingmodelType= " log4net. Appender.fileappender+minimallock "/> To use the minimal locking model (minimal locking models) to allow multiple processes to write to the same file.


Various Appender Description:

In Log4net configuration, Appender is the most important part, in general, each appender represents a log output media, such as log files, Evenglog, database, console, mail, ASP. NET pages, and so on.

This article provides examples of the various built-in appender configurations, but is far from exhaustive. For instructions on the parameters and options for each appender, refer to the Appender SDK documentation.
The following example is done under. NET 2.0, and the log4net version is 1.2.10.

Adonetappender

Details refer to log4net. Appender. Adonetappender SDK documentation.

The configuration content of the Adonetappender depends on the provider of the target database. The following examples are provided for SQL Server 2000 only.

First set up the data table:

CREATE TABLE [dbo]. [Log]
(
[ID] [int] IDENTITY (1, 1) not NULL,
[Date]   [DateTime] Not NULL,
[Thread]  [varchar] (255) Not NULL,
[Level]  [varchar] () not NULL,
[Logger]  [varchar] (255) Not NULL,
[Message]  [varchar] (4000) Not NULL,
[Exception]  [varchar] (Watts) NULL
)
Then add the configuration:

< Appender name = "Adonetappender" type = log4net. Appender.adonetappender ">
< buffersize value = "2"/>
< ConnectionType value = "System.Data.SqlClient.SqlConnection, System.Data, version=2.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 "/>
< connectionString value = "server= (local);d the atabase=testbase;integrated security=false;persist security info=true; Uid=sa; Pwd= "/>
< CommandText value = "INSERT into Log ([date],[thread],[level],[logger],[message],[exception]) VALUES (@log_date, @ Thread, @log_level, @logger, @message, @exception) "/>
< parameter >
< parametername value = "@log_date"/>
< DbType value = "DateTime"/>
< layout type = "Log4net." Layout.rawtimestamplayout "/>
</parameter >
< parameter >
< parametername value = "@thread"/>
< DbType value = "String"/>
< size value = "255"/>
< layout type = "Log4net." Layout.patternlayout ">
< Conversionpattern value = "%thread"/>
</Layout >
</parameter >
< parameter >
< parametername value = "@log_level"/>
< DbType value = "String"/>
< size value = "/>"
< layout type = "Log4net." Layout.patternlayout ">
< Conversionpattern value = "%level"/>
</Layout >
</parameter >
< parameter >
< parametername value = "@logger"/>
< DbType value = "String"/>
< size value = "255"/>
< layout type = "Log4net." Layout.patternlayout ">
< Conversionpattern value = "%logger"/>
</Layout >
</parameter >
< parameter >
< parametername value = "@message"/>
< DbType value = "String"/>
< size value = "4000"/>
< layout type = "Log4net." Layout.patternlayout ">
< Conversionpattern value = "%message"/>
</Layout >
</parameter >
< parameter >
< parametername value = "@exception"/>
< DbType value = "String"/>
< size value = "/>"
< layout type = "Log4net." Layout.exceptionlayout "/>
</parameter >
</Appender >
BufferSize represents a batch of log events that avoids accessing the database for each log event, ConnectionType specifies the fully qualified type name of the IDbConnection to use, and ConnectionString represents the connection string; CommandText is an SQL statement or stored procedure, and the last set of parameter nodes describes the parameters required by the SQL statement or stored procedure.

Aspnettraceappender details refer to log4net. Appender.aspnettraceappender SDK documentation.

< Appender name = "Aspnettraceappender" type = log4net. Appender.aspnettraceappender ">
< layout type = "Log4net." Layout.patternlayout ">
< Conversionpattern value = "%date [%thread]%-5level%logger [%PROPERTY{NDC}]-%message%newline"/>
</Layout >
</Appender >
This configuration prints log information to the trace context environment of the page. If the log level is below Warn, is output in the System.Web.TraceContext.Write method, or if the level is warn or warn above, the System.Web.TraceContext.Warn method outputs, and the different colors of the log information in the following illustration illustrate this. The effect chart is as follows:



This is very convenient when you are debugging the page.

BufferingforwardingappenderDetails refer to log4net. Appender.bufferingforwardingappender SDK documentation.

< Appender name = "Bufferingforwardingappender" type = log4net. Appender.bufferingforwardingappender ">
< buffersize value = "5"/>
< lossy value = "true"/>
< evaluator type = "log4net." Core.levelevaluator ">
< threshold value = "WARN"/>
</Evaluator >
< Appender-ref ref = "Logfileappender"/>
< Appender-ref ref = "Aspnettraceappender"/>
</Appender >
The primary role of Bufferingforwardingappender is to cache the log information of the appender that is output to the specified type (here is Logfileappender). The BufferSize property specifies the number of caches, and if value is 5, the log is output in batches when the amount of information reaches 6. The Appender-ref property specifies the cached Appender type, which, like the root node, can specify more than one.

Coloredconsoleappender

Details refer to log4net. Appender.coloredconsoleappender SDK documentation. Coloredconsoleappender Prints log information to the console. By default, log information is sent to the console standard output stream. The following example shows how to highlight error information.

< Appender name = "Coloredconsoleappender" type = log4net. Appender.coloredconsoleappender "

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.