Log4net 基本样式:

 <log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
    <param name="File" value="Mylog/" />
    <param name="AppenderToFile" value="true" />
    <layout type="log4net.Layout.PatternLayout">
        <param name="Header" value="[Header]\r\n" />
        <param name="Footer" value="[Footer]\r\n" />
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
    </layout>
    <!--过滤器:将只记录level为DEBUG与WARN之间的日志-->
    <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG">
        <param name="LevelMax" value="WARN">
    </filter>
</appender>

<root>
    <!--缺省为DEBUG级别-->
    <level value="DEBUG">
    <!--声明了在其他地方定义的Appender对象的一个引用-->
    <appender-ref ref="LogFileAppender">

</root>
<!--    logger标签不是必须的   Additivity的值缺省是true-->
<logger name="testApp.Logging" additivity="false">
<level value="DEBUG" />
</logger>
</log4net>

Log4net.config

Log4Net配置文件的书写

添加section

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>

按日期分隔文件

    <!-- 按日期分隔文件 -->
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value="D:\\MyLogFiles\\ClientService\log4net\"/>
    <!-- 是否在文件中追加 -->
    <param name="AppendToFile" value="true"/>
    <!-- 最小锁定模型以允许多个进程可以写入同一个文件 -->
    <param name="LockingModel" value="log4net.Appender.FileAppender.MinimalLock" />
    <!--最多生成(MaxSizeRollBackups的值+1)个文件。如果注释掉,那么只生成一个文件。到了轮训条件,会从最早的开始覆盖以前的信息-->
    <!-- 好像只有按Size分隔文件,才有效果 -->
    <param name="MaxSizeRollBackups" value="31"/>
    <!--如果RollingStyle为Size,这里需要填写正数加上单位(KB,MB)-->
    <param name="MaximumFileSize" value="-1"/>
    <!--轮询方式,文件大小,超过MaximumFileSize的值,则重新生成一个文件-->
    <!--<param name="RollingStyle" value="Size"/>-->
    <!--轮询方式,日期,每天生成一个文件。非当天的文件,会在File的值后面加上20151215-->
    <param name="RollingStyle" value="Date"/>
    <param name="StaticLogFileName" value="false"/>
    <param name="datePattern" value="'zsyylog'yyyyMMdd'.txt'" />
    <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%m%n"/>
    </layout>
    </appender>

添加到MSSQL数据库

    <appender name="AdoNetAppender_SQLServer" type="log4net.Appender.AdoNetAppender">
      <!-- 缓存大小 -->
      <bufferSize value="300" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="SERVER=10.10.4.85;UID=zsyy;Pwd=zsyy*123;DATABASE=HandheldYonYou" />
      <commandText value="INSERT INTO [HandheldYonYou].[dbo].[Log4net_Log]([Date],[RequestUID],[RequestPort],[RequestUri],[StringType],[RequestMethod],[ApiVersion],[Message],[Thread],[Level],[Logger],[Exception])
     VALUES(@log_date ,@RequestUID ,@RequestPort ,@RequestUri,@StringType,@RequestMethod,@ApiVersion,@message ,@thread ,@log_level ,@logger ,@exception )" />
      <parameter>
        <!-- 字段名称 -->
        <parameterName value="@RequestUID" />
        <!-- 字段数据类型 -->
        <dbType value="String" />
        <!-- 字段数据长度 -->
        <size value="50"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{RequestUID}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@RequestPort" />
        <dbType value="String" />
        <size value="50"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{RequestPort}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@RequestUri" />
        <dbType value="String" />
        <size value="2000"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{RequestUri}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@StringType" />
        <dbType value="String" />
        <size value="50"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{StringType}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@RequestMethod" />
        <dbType value="String" />
        <size value="50"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{RequestMethod}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@ApiVersion" />
        <dbType value="String" />
        <size value="50"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{ApiVersion}" />
        </layout>
      </parameter>
      <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>

记录日志的方式

    <root>
      <!-- 日志的等级(ALL、DEBUG、WRONG、ERROR、INFO) -->
      <level value="ALL"/>
      <!-- 将日志记录到SQL -->
      <appender-ref ref="AdoNetAppender_SQLServer"/>
      <!-- 循环将日志添加到文件 -->
      <appender-ref ref="RollingFileAppender"/>
    </root>
05-11 11:17