Log4net 使用文档例子

Log4net 使用文档例子

一般我们都会使用数据库和文件作为log的输出介质,下面是我在项目实践中总结到的经验:
1 .ADONetAppender
( 1 )、 MS SQL Server
define the table such as:
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
)

<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>
( 2 )、 MS Access  please reference the SDK of Log4net document   
( 3 )、 Oracle9i                                     log4net/release/config-examples.html
( 4 )、 Oracle8i
( 5 )、 DB2

2 、 FileAppender
<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>

4.  RemotingAppender
传送日志事件到达一个特定的接受器,sink定义了这个接受器的地址,lossy定义了没有丢弃事件,bufferSize 定义了一个black的由95个事件打包发送的,而onlyFixPartialEventData可以忽律一些特定的事件。
<appender name="RemotingAppender" type="log4net.Appender.RemotingAppender" >
       <sink value="tcp://localhost:8085/LoggingSink" />
       <lossy value="false" />
       <bufferSize value="95" />
      <onlyFixPartialEventData value="true" />
</appender>

5.  RollingFileAppender
由于定义了staticLogFileName 为true,以我的理解就是定位保存的log文件个数为10个(maxSizeRollBackups),同时设定了每个文件的大小为100KB,所以一直都保持有10个文件。RollingStyle设定rolling的触发器。
Eg1:
<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>

  eg2: 
<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>

eg3:

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

6.  UdpAppender
可以发送到远程地址的特定端口
<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> 
log4net 的学习暂时告一段落了,希望能够进一步对log4j学习,现在我越来越喜欢.net的东西了,再也不会感觉到很别扭!希望自己可以很快地强大起来!!! 

你可能感兴趣的:(Log4net 使用文档例子)