log4net保存到数据库系列二:独立配置文件中配置log4net

园子里面有很多关于log4net保存到数据库的帖子,但是要动手操作还是比较不易,从头开始学习log4net数据库日志一、WebConfig中配置log4net

 

 

下面先一点一点来学习如果进行配置

1.下载,2.数据库脚本,请参考log4net保存到数据库系列一:WebConfig中配置log4net

3.配置文件

log4net.config文件属性为嵌入的资源,始终复制

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
<log4net>
    <!--注册Appender-->
    <root>
      <level value="ALL" />
      <appender-ref ref="AdoNetAppender" />
    </root>
    <!--配置Appender-->
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="-1" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="data source=[服务器地址];initial catalog=[数据库];persist security info=True;user id=[账号];password=[密码];MultipleActiveResultSets=True;" />
      <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>
  </log4net>
</configuration>
log4net.config配置文件

 

 

4.设置监控

AssemblyInfo.cs中增加代码

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

  

 5.测试代码

View Code

 

 6.测试成功

 

 

posted @ 2016-07-18 21:04  金钱  阅读(1165)  评论(0编辑  收藏  举报