在log4net的配置中,appender是最重要的部分,一般来说,每一种appender都表示一种日志的输出介质,如日志文件、EvengLog、数据库、控制台、邮件、ASP.NET页面等。
本文对各种内置的appender的配置提供了示例,但却远称不上详尽。要想了解每一种appender的参数和选项的说明,请参看该appender的SDK文档。
AdoNetAppender
详情参考 log4net.Appender. AdoNetAppender SDK文档。
AdoNetAppender的相关配置内容取决于目标数据库的provider。下面仅提供SQL Server 2000的例子。
首先建立数据表:
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
)
(
[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
)
然后添加配置:
<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);database=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="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>
<bufferSize value="2" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=Neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="server=(local);database=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="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>
bufferSize表示批处理的日志事件,可以避免每次日志事件都访问数据库;ConnectionType指定了要使用的IDbConnection的完全限定类型名称;connectionString表示连接字符串;CommandText是SQL语句或存储过程;最后一组parameter节点描述了SQL语句或存储过程需要的参数。
AspNetTraceAppender
详情参考 log4net.Appender.AspNetTraceAppender SDK 文档。
<appender name="AspNetTraceAppender" type="log4net.Appender.AspNetTraceAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
这段配置可将日志信息输出到页面的Trace上下文环境。如果日志的级别低于WARN,会以System.Web.TraceContext.Write方法输出;如果级别为WARN或WARN以上则会以System.Web.TraceContext.Warn方法输出,下图中的日志信息的不同颜色可以说明这一点。 效果图如下:
这在进行页面调试的时候可是很方便的。
BufferingForwardingAppender
详情参考 log4net.Appender.BufferingForwardingAppender SDK 文档。
<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>
<bufferSize value="5"/>
<lossy value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN"/>
</evaluator>
<appender-ref ref="LogFileAppender" />
<appender-ref ref="AspNetTraceAppender" />
</appender>
BufferingForwardingAppender的主要作用是将输出到指定类型(这里是LogFileAppender)的Appender的日志信息进行缓存。bufferSize属性指定了缓存的数量,如果value为5,那么将在信息量达到6条的时候,把这些日志批量输出。appender-ref属性指定了缓存的Appender类型,同root节点一样,这里可以指定多个。
ColoredConsoleAppender
详情参考log4net.Appender.ColoredConsoleAppender SDK 文档。
ColoredConsoleAppender将日志信息输出到控制台。默认情况下,日志信息被发送到控制台标准输出流。下面这个示例演示了如何高亮显示Error信息。
<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>
<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>
效果如下:
还可以为不同的级别指定不同的颜色:
<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>
<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
详情参考 log4net.Appender.ConsoleAppender SDK 文档。
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
EventLogAppender
详情参考 log4net.Appender.EventLogAppender SDK 文档。
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
FileAppender
详情参考 log4net.Appender.File Appender SDK 文档。
FileAppender将日志信息输出到指定的日志文件。
<appender name="LogFileAppender" type="l