log4net的各种Appender配置示例
Apache log4net™ Config Examples
Overview
This document presents example configurations for the built-in appenders. These configurations are designed to work with the log4net.Config.DOMConfigurator and the log4net.Repository.Hierarchy.Hierarchy.
These examples are by no means exhaustive configurations for the appenders. For a full list of the parameters that can be specified to each appender and more details on each options see the SDK documentation for the appender.
AdoNetAppender
For full details see the SDK Reference entry: log4net.Appender.AdoNetAppender.
The configuration of the AdoNetAppender depends on the provider selected for the target database. Here are some examples.
MS SQL Server
The following example shows how to configure the AdoNetAppender to log messages to a SQL Server database. The events are written in batches of 100 (BufferSize). The ConnectionType specifies the fully qualified type name for the System.Data.IDbConnection to use to connect to the database. The ConnectionString is database provider specific. The CommandText is either a prepared statement or a stored procedure, in this case it is a prepared statement. Each parameter to the prepared statement or stored procedure is specified with its name, database type and a layout that renders the value for the parameter.
The database table definition is:
CREATE TABLE [dbo].[Log] (
[Id] [int] IDENTITY (1, 1) NOT NULL,
[Date] [datetime] NOT NULL,
[Thread] [varchar] (255) NOT NULL,
[Level] [varchar] (50) NOT NULL,
[Logger] [varchar] (255) NOT NULL,
[Message] [varchar] (4000) NOT NULL,
[Exception] [varchar] (2000) NULL
)
The appender configuration is:
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="100" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=[database server];initial catalog=[database name];integrated security=false;persist security info=True;User ID=[user];Password=[password]" />
<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="50" />
<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="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
MS Access
This example shows how to write events to an Access Database.
<appender name="AdoNetAppender_Access" type="log4net.Appender.AdoNetAppender">
<connectionString value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\log\access.mdb;User Id=;Password=;" />
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date" />
</layout>
</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="50" />
<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="1024" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
</appender>
Oracle9i
This example shows how to write events to an Oracle9i Database.
The database table definition is:
create table log (
Datetime timestamp(3),
Thread varchar2(255),
Log_Level varchar2(255),
Logger varchar2(255),
Message varchar2(4000)
);
The appender configuration is:
<appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender">
<connectionType value="System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=[mydatabase];User ID=[user];Password=[password]" />
<commandText value="INSERT INTO Log (Datetime,Thread,Log_Level,Logger,Message) VALUES (:log_date, :thread, :log_level, :logger, :message)" />
<bufferSize value="128" />
<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="50" />
<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>
</appender>
Oracle8i
This example shows how to write events to an Oracle8i Database.
The database table definition is:
CREATE TABLE CSAX30.LOG
(
THREAD VARCHAR2(255),
LOG_LEVEL VARCHAR2(255),
LOGGER VARCHAR2(255),
MESSAGE VARCHAR2(4000)
) TABLESPACE CSAX30D LOGGING
The appender configuration is:
<appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender">
<connectionType value ="System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=<dsname>;User ID=<userid>;Password=<password>" />
<commandText value="INSERT INTO Log (Log_Level,Logger,Message) VALUES (:log_level, :logger, :message)" />
<bufferSize value="250" />
<parameter>
<parameterName value=":log_level" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value=":logger" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value=":message" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
</appender>
IBM DB2
This example shows how to write events to an IBM DB2 8.2 Database. The following syntax should also work with older DB2 database servers.
The database table definition is:
CREATE TABLE "myschema.LOG" (
"ID" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (
START WITH +1
INCREMENT BY +1
MINVALUE +1
MAXVALUE +2147483647
NO CYCLE
NO CACHE
NO ORDER
),
"DATE" TIMESTAMP NOT NULL,
"THREAD" VARCHAR(255) NOT NULL,
"LEVEL" VARCHAR(500) NOT NULL,
"LOGGER" VARCHAR(255) NOT NULL,
"MESSAGE" VARCHAR(4000) NOT NULL,
"EXCEPTION" VARCHAR(2000)
)
IN "LRGTABLES";
The appender configuration is:
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="100" />
<connectionType value="IBM.Data.DB2.DB2Connection,IBM.Data.DB2, Version=8.1.2.1" />
<connectionString value="server=192.168.0.0;database=dbuser;user Id=username;password=password;persist security info=true" />
<commandText value="INSERT INTO myschema.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="500" />
<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="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
SQLite
This example shows how to write events to a SQLite Database. This was tested against v0.21 of the SQLite .NET provider.
SQLite doesn't have strongly-typed columns or field lengths but its recommended you still include this information for readability. The database table definition is:
CREATE TABLE Log (
LogId INTEGER PRIMARY KEY,
Date DATETIME NOT NULL,
Level VARCHAR(50) NOT NULL,
Logger VARCHAR(255) NOT NULL,
Message TEXT DEFAULT NULL
);
The appender configuration is:
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="100" />
<connectionType value="Finisar.SQLite.SQLiteConnection, SQLite.NET, Version=0.21.1869.3794, Culture=neutral, PublicKeyToken=c273bd375e695f9c" />
<connectionString value="Data Source=c:\\inetpub\\wwwroot\\logs\\log4net.db;Version=3;" />
<commandText value="INSERT INTO Log (Date, Level, Logger, Message) VALUES (@Date, @Level, @Logger, @Message)" />
<parameter>
<parameterName value="@Date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@Level" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@Logger" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@Message" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
</appender>
AspNetTraceAppender
For full details see the SDK Reference entry: log4net.Appender.AspNetTraceAppender.
The following example shows how to configure the AspNetTraceAppender to log messages to the ASP.NET TraceContext. The messages are written to the System.Web.TraceContext.Write method if they are below level WARN. If they are WARN or above they are written to the System.Web.TraceContext.Warn method.
<appender name="AspNetTraceAppender" type="log4net.Appender.AspNetTraceAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
BufferingForwardingAppender
For full details see the SDK Reference entry: log4net.Appender.BufferingForwardingAppender.
The following example shows how to configure the BufferingForwardingAppender to buffer 100 messages before delivering them to the ConsoleAppender.
<appender name="BufferingForwardingAppender" type="log4net.Appender.BufferingForwardingAppender" >
<bufferSize value="100"/>
<appender-ref ref="ConsoleAppender" />
</appender>
This example shows how to deliver only significant events. A LevelEvaluator is specified with a threshold of WARN. This means that the events will only be delivered when a message with level of WARN or higher level is logged. Up to 512 (BufferSize) previous messages of any level will also be delivered to provide context information. Messages not sent will be discarded.
<appender name="BufferingForwardingAppender" type="log4net.Appender.BufferingForwardingAppender" >
<bufferSize value="512" />
<lossy value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN"/>
</evaluator>
<appender-ref ref="ConsoleAppender" />
</appender>
ColoredConsoleAppender
For full details see the SDK Reference entry: log4net.Appender.ColoredConsoleAppender.
The following example shows how to configure the ColoredConsoleAppender to log messages to the console. By default the messages are sent to the console standard output stream. This example shows how to highlight error messages.
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="White" />
<backColor value="Red, HighIntensity" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
This example shows how to colorize multiple levels.
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="White" />
<backColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="DEBUG" />
<backColor value="Green" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
ConsoleAppender
For full details see the SDK Reference entry: log4net.Appender.ConsoleAppender.
The following example shows how to configure the ConsoleAppender to log messages to the console. By default the messages are sent to the console standard output stream.
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
This example shows how to direct the log messages to the console error stream.
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<target value="Console.Error" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
EventLogAppender
For full details see the SDK Reference entry: log4net.Appender.EventLogAppender.
The following example shows how to configure the EventLogAppender to log to the Application event log on the local machine using the event Source of the AppDomain.FriendlyName.
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
This example shows how to configure the EventLogAppender to use a specific event Source.
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<applicationName value="MyApp" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
For more information on how to setup the event log to allow the EventLogAppender to write to it, see the FAQ: Why doesn't the EventLogAppender work?.
FileAppender
For full details see the SDK Reference entry: log4net.Appender.FileAppender.
The following example shows how to configure the FileAppender to write messages to a file. The file specified is log-file.txt. The file will be appended to rather than overwritten each time the logging process starts.
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
This example shows how to configure the file name to write to using an environment variable TMP. The encoding to use to write to the file is also specified.
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="${TMP}\log-file.txt" />
<appendToFile value="true" />
<encoding value="unicodeFFFE" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
This example shows how to configure the appender to use the minimal locking model that allows multiple processes to write to the same file.
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="${TMP}\log-file.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
This example shows how to configure the appender to use the "inter process" locking model.
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="${TMP}\log-file.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+InterProcessLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
Before you change the locking model so that multiple process may log to the same file, please read the FAQ.
ForwardingAppender
For full details see the SDK Reference entry: log4net.Appender.ForwardingAppender.
The following example shows how to configure the ForwardingAppender. The forwarding appender allows a set of constraints to be used to decorate an appender. In this example the ConsoleAppender is decorated with a Threshold of level WARN. This means that an event directed to the ConsoleAppender directly will be logged regardless of its level, but an event directed to the ForwardingAppender will only be passed on to the ConsoleAppender if its level is WARN or higher. This appender is used only in special circumstances.
<appender name="ForwardingAppender" type="log4net.Appender.ForwardingAppender" >
<threshold value="WARN"/>
<appender-ref ref="ConsoleAppender" />
</appender>
ManagedColoredConsoleAppender
For full details see the SDK Reference entry: log4net.Appender.ManagedColoredConsoleAppender.
The following example shows how to configure the ManagedColoredConsoleAppender to log messages to the console. By default the messages are sent to the console standard output stream. This example shows how to highlight error messages.
<appender name="ManagedColoredConsoleAppender" type="log4net.Appender.ManagedColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="White" />
<backColor value="Red" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
This example shows how to colorize multiple levels.
<appender name="ManagedColoredConsoleAppender" type="log4net.Appender.ManagedColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="DarkRed" />
</mapping>
<mapping>
<level value="WARN" />
<foreColor value="Yellow" />
</mapping>
<mapping>
<level value="INFO" />
<foreColor value="White" />
</mapping>
<mapping>
<level value="DEBUG" />
<foreColor value="Blue" />
</mapping> <layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %-20.20logger: %message%newline"/>
</layout>
</appender>
MemoryAppender
For full details see the SDK Reference entry: log4net.Appender.MemoryAppender.
It is unlikely that the MemoryAppender will be configured using a config file, but if you want to do it here's how.
<appender name="MemoryAppender" type="log4net.Appender.MemoryAppender">
<onlyFixPartialEventData value="true" />
</appender>
NetSendAppender
For full details see the SDK Reference entry: log4net.Appender.NetSendAppender.
The following example shows how to configure the NetSendAppender to deliver messages to a specific user's screen. As this appender is typically only used for important notifications a Threshold of level Error is specified. This example delivers the messages to the user nicko on the machine SQUARE. However things are not always straight forward using the Windows Messenger Service, one possible outcome using this configuration is that the Server will broadcast looking for a WINS server which it will then ask to deliver the message to the Recipient, the WINS server will deliver it to the first terminal that the user logged in from.
<appender name="NetSendAppender" type="log4net.Appender.NetSendAppender">
<threshold value="ERROR" />
<server value="SQUARE" />
<recipient value="nicko" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
OutputDebugStringAppender
For full details see the SDK Reference entry: log4net.Appender.OutputDebugStringAppender.
The following example shows how to configure the OutputDebugStringAppender to write logging messages to the OutputDebugString API.
<appender name="OutputDebugStringAppender" type="log4net.Appender.OutputDebugStringAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
RemotingAppender
For full details see the SDK Reference entry: log4net.Appender.RemotingAppender.
The following example shows how to configure the RemotingAppender to deliver logging events to a specified Sink (in this example the sink is tcp://localhost:8085/LoggingSink). In this example the events are delivered in blocks of 95 events because of the BufferSize. No events are discarded. The OnlyFixPartialEventData option allows the appender to ignore certain logging event properties that can be very slow to generate (e.g. the calling location information).
<appender name="RemotingAppender" type="log4net.Appender.RemotingAppender" >
<sink value="tcp://localhost:8085/LoggingSink" />
<lossy value="false" />
<bufferSize value="95" />
<onlyFixPartialEventData value="true" />
</appender>
This example configures the RemotingAppender to deliver the events only when an event with level ERROR or above is logged. When the events are delivered, up to 200 (BufferSize) previous events (regardless of level) will be delivered to provide context. Events not delivered will be discarded.
<appender name="RemotingAppender" type="log4net.Appender.RemotingAppender" >
<sink value="tcp://localhost:8085/LoggingSink" />
<lossy value="true" />
<bufferSize value="200" />
<onlyFixPartialEventData value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="ERROR"/>
</evaluator>
</appender>
RollingFileAppender
For full details see the SDK Reference entry: log4net.Appender.RollingFileAppender.
The RollingFileAppender builds on the FileAppender and has the same options as that appender.
The following example shows how to configure the RollingFileAppender to write to the file log.txt. The file written to will always be called log.txt because the StaticLogFileName param is specified. The file will be rolled based on a size constraint (RollingStyle). Up to 10 (MaxSizeRollBackups) old files of 100 KB each (MaximumFileSize) will be kept. These rolled files will be named: log.txt.1, log.txt.2, log.txt.3, etc...
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
This example show how to configure the RollingFileAppender to roll log files on a date period. This example will roll the log file every minute! To change the rolling period adjust the DatePattern value. For example, a date pattern of "yyyyMMdd" will roll every day. See System.Globalization.DateTimeFormatInfo for a list of available patterns.
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logfile" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd-HHmm" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
This example show how to configure the RollingFileAppender to roll log files on a date period and within a date period on file size. For each day only the last 10 files of 1MB will be kept.
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logfile" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
This example show how to configure the RollingFileAppender to roll log files once per program execution. The appendToFile property is set to false to prevent the appender from overwriting the existing files. The maxSizeRollBackups is set to negative 1 to allow an infinite number of backup files. The file size does have to be limited but here it is set to 50 Gigabytes which, if a log file exceeds this size limit during a single run then it will also be rolled.
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logfile.txt" />
<appendToFile value="false" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="-1" />
<maximumFileSize value="50GB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
SmtpAppender
For full details see the SDK Reference entry: log4net.Appender.SmtpAppender.
The following example shows how to configure the SmtpAppender to deliver log events via SMTP email. The To, From, Subject and SmtpHost are required parameters. This example shows how to deliver only significant events. A LevelEvaluator is specified with a threshold of WARN. This means that an email will be sent for each WARN or higher level message that is logged. Each email will also contain up to 512 (BufferSize) previous messages of any level to provide context. Messages not sent will be discarded.
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
<to value="to@domain.com" />
<from value="from@domain.com" />
<subject value="test logging message" />
<smtpHost value="SMTPServer.domain.com" />
<bufferSize value="512" />
<lossy value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN"/>
</evaluator>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
</layout>
</appender>
This example shows how to configure the SmtpAppender to deliver all messages in emails with 512 (BufferSize) messages per email.
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
<to value="to@domain.com" />
<from value="from@domain.com" />
<subject value="test logging message" />
<smtpHost value="SMTPServer.domain.com" />
<bufferSize value="512" />
<lossy value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
</layout>
</appender>
This example shows a more verbose formatting layout for the mail messages.
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender,log4net">
<to value="to@domain.com" />
<from value="from@domain.com" />
<subject value="test logging message" />
<smtpHost value="SMTPServer.domain.com" />
<bufferSize value="512" />
<lossy value="false" />
<evaluator type="log4net.Core.LevelEvaluator,log4net">
<threshold value="WARN" />
</evaluator>
<layout type="log4net.Layout.PatternLayout,log4net">
<conversionPattern value="%property{log4net:HostName} :: %level :: %message %newlineLogger: %logger%newlineThread: %thread%newlineDate: %date%newlineNDC: %property{NDC}%newline%newline" />
</layout>
</appender>
SmtpPickupDirAppender
For full details see the SDK Reference entry: log4net.Appender.SmtpPickupDirAppender.
The SmtpPickupDirAppender is configured similarly to the SmtpAppender. The only difference is that rather than specify a SmtpHost parameter a PickupDir must be specified.
The PickupDir parameter is a path that must exist and the code executing the appender must have permission to create new files and write to them in this directory. The path is relative to the application's base directory (AppDomain.BaseDirectory).
The following example shows how to configure the SmtpPickupDirAppender to deliver log events via SMTP email. The To, From, Subject and PickupDir are required parameters. This example shows how to deliver only significant events. A LevelEvaluator is specified with a threshold of WARN. This means that an email will be sent for each WARN or higher level message that is logged. Each email will also contain up to 512 (BufferSize) previous messages of any level to provide context. Messages not sent will be discarded.
<appender name="SmtpPickupDirAppender" type="log4net.Appender.SmtpPickupDirAppender">
<to value="to@domain.com" />
<from value="from@domain.com" />
<subject value="test logging message" />
<pickupDir value="C:\SmtpPickup" />
<bufferSize value="512" />
<lossy value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN"/>
</evaluator>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
</layout>
</appender>
TraceAppender
For full details see the SDK Reference entry: log4net.Appender.TraceAppender.
The following example shows how to configure the TraceAppender to log messages to the System.Diagnostics.Trace system. This is the tracing system supplied with the .net base class libraries. See the MSDN documentation for the System.Diagnostics.Trace class for more details on how to configure the trace system.
<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
UdpAppender
For full details see the SDK Reference entry: log4net.Appender.UdpAppender.
The following example shows how to configure the UdpAppender to send events to a RemoteAddress on the specified RemotePort.
<appender name="UdpAppender" type="log4net.Appender.UdpAppender">
<localPort value="8080" />
<remoteAddress value="224.0.0.1" />
<remotePort value="8080" />
<layout type="log4net.Layout.PatternLayout, log4net">
<conversionPattern value="%-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
DynamicPatternLayout
For full details see the SDK Reference entry: log4net.Layout.DynamicPatternLayout.
The DynamicPatternLayout should be used whenever the header or footer should contain information that could change over time. Compared to the static PatternLayout which does not re-evaluate on every invoke, the DynamicPatternLayout does re-evaluate the pattern on every invoke. It does, for example, allow to include the current DateTime in the header and/or the footer which was not possible with the static PatternLayout.
The following example shows how to configure the DynamicPatternLayout.
<layout type="log4net.Layout.DynamicPatternLayout">
<param name="Header" value="%newline**** Trace Opened Local: %date{yyyy-MM-dd HH:mm:ss.fff} UTC: %utcdate{yyyy-MM-dd HH:mm:ss.fff} ****%newline"/>
<param name="Footer" value="**** Trace Closed %date{yyyy-MM-dd HH:mm:ss.fff} ****%newline"/>
</layout>
原文来自:http://logging.apache.org/log4net/release/config-examples.html#smtpappender
也可参见中文:http://www.cnblogs.com/anderslly/archive/2007/03/09/log4netconfigsamples.html
log4net的各种Appender配置示例的更多相关文章
- 关于Log4Net的使用及配置方式
目录 0.简介 1.安装程序包 2.配置文件示例 3.日记的级别:Level 4.日志的输出源:Appenders 5.日志格式:Layout 6.日志文件变换方式(回滚方式):RollingStyl ...
- log4net 日志框架的配置
log4net 日志框架的配置——静态文件(一) 添加对log4net程序集的引用 选择程序集文件添加引用即可,需要注意的是需要添加相应程序版本的程序集,如果你的应用是基于.netFramework2 ...
- Log4Net写入到数据库配置过程中的一些小问题备忘
问题1: 在公司进行log4net写入服务器配置的时候,一切正常,但是在家里的机器上,就频繁出现这个问题: SQL Server 2008 报错:已成功与服务器建立连接,但是在登录前的握手期间发生错误 ...
- java日志规约及配置示例终极总结
目录 什么是日志 常用日志框架 日志级别详解 日志的记录时机 日志使用规约 logback 配置示例 loh4j2 配置示例 什么是日志? 简单的说,日志就是记录程序的运行轨迹,方便查找关键信息,也方 ...
- spring boot 日志介绍 以及 logback配置示例
https://www.cnblogs.com/flying607/p/7827460.html 以下是springboot的一个局部依赖关系: 可以看到,java util logging(jul) ...
- Log4Net日志的简单使用示例
前言 源码参考示例地址 http://www.51aspx.com/Code/log4netusedemo/2707 本例博客园源码 https://files.cnblogs.com/files/m ...
- Nginx 简单的负载均衡配置示例(转载)
原文地址:Nginx 简单的负载均衡配置示例(转载) 作者:水中游于 www.s135.com 和 blog.s135.com 域名均指向 Nginx 所在的服务器IP. 用户访问http://www ...
- HBase + Kerberos 配置示例(二)
接上篇<HBase + Kerberos配置示例(一)>,我们继续剩下的配置工作. 环境准备 Hadoop配置 Zookeeper配置 HBase配置 Java测试程序 环境准备 安装ha ...
- 全互联结构DVPN综合配置示例
以下内容摘自正在全面热销的最新网络设备图书“豪华四件套”之一<H3C路由器配置与管理完全手册>(第二版)(其余三本分别是:<Cisco交换机配置与管理完全手册>(第二版).&l ...
随机推荐
- ILGenerator.Emit动态 MSIL编程(三)之动态代理
using System; using System.Linq; using System.Reflection; using System.Reflection.Emit; public seale ...
- NSUserDefault的使用
NSUserDefaults 在我们编写代码中是最常用的一个永久保存数据的方法,也是最简单的. 使用NSUserDefault需要注意: 1.数据的本地化保存不是实时的,如果需要实时保存,调用sync ...
- 常用Web Service汇总(天气预报、时刻表等)
现成的Web Service中有很多很好用的,比如天气预报,IP地址搜索,火车时刻表等等.本文汇总的一些常用Web Service,希望对大家有所帮助. AD: ================= ...
- mybatis实战教程(mybatis in action)之四:实现关联数据的查询
有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如最常见到的多对一,一对多等.这些查询是如何处理的呢,这一讲就讲这个问题.我们首先创建一个Article 这个表 ...
- PowerProfile.java与power_profile.xml
转载自http://blog.csdn.net/green1900/article/details/42427871 现在诸多关于电池管理的应用做的极其绚烂,可实现如耗电应用排行.剩余时间计算.关闭耗 ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- TP-link TL-WN725 USB无线网卡在DX2 CPU下的Xlinux 驱动移植
网上查到,TP-link TL-WN725 USB无线网卡用的芯片是8188EU 下载驱动:http://download.csdn.net/detail/zzz_mraz/5262010 tar z ...
- Linux内核模块设计
内核的设计有两种方式:单内核和微内核,两者各有优劣,关于两者的比较可以参见wiki.windowds和Solaris采用微内核结构. Linux内核采用单内核结构,设计比较简单,但单内核的理念是把所有 ...
- 牛客网程序员面试金典:1.1确定字符互异(java实现)
问题描述: 请实现一个算法,确定一个字符串的所有字符是否全都不同.这里我们要求不允许使用额外的存储结构. 给定一个string iniString,请返回一个bool值,True代表所有字符全都不同, ...
- 自己用c语言实现字符串处理库函数以及扩展
1.实现基本的c语言库函数: int myStrlen( const char* str);//根据传入的字符串首地址获取字符串长度:返回值为长度 int myStrlen(const char* s ...