1

我有一个应用程序,其中简单的文本文件日志记录工作正常。现在我还需要登录 mysql 数据库。但是在我的代码日志条目之后什么也没发生。我的 log4net 配置是

<?xml version="1.0" encoding="utf-8" ?>
<log4net>  
  <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
    <immediateFlush value="true" />
    <bufferSize value="1" />
    <connectionType value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data, Version=6.4.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    <connectionString value="data source=localhost;initial catalog=mytestdb;User ID=root;Password=" />
    <commandText value="INSERT INTO system_log(thread,level,appname,message,action) VALUES (?thread, ?level, ?appname, ?message, ?action)" />   
    <parameter>
      <parameterName value="thread" />
      <dbType value="String" />
      <size value="100" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%t" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="level" />
      <dbType value="String" />
      <size value="10" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%p" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="appname" />
      <dbType value="String" />
      <size value="10" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="My Web Service" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="message" />
      <dbType value="String" />
      <size value="4000" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%m" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="action" />
      <dbType value="String" />
      <size value="45" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%5c{1}.%M" />
      </layout>
    </parameter>
    <filter type="log4net.Filter.LevelRangeFilter">
      <acceptOnMatch value="true" />
      <levelMin value="DEBUG" />
      <levelMax value="FATAL" />
    </filter>
  </appender>
  <!--<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender" >
    <file value="..\\Logs.txt" />
    <appendToFile value="true" />
    <datePattern value="yyyyMMdd" />
    <rollingStyle value="Date" />
    <filter type="log4net.Filter.LevelRangeFilter">
      <acceptOnMatch value="true" />
      <levelMin value="DEBUG" />
      <levelMax value="FATAL" />
    </filter>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%d{yyyy-MM-dd HH:mm:ss.fff} %10p %-5property{_App} %-5property{_Node} [%2t] %5c{1}.%M - %m%n" />
    </layout>
  </appender>-->
  <root>  
    <level value="ALL" />    
     <!--<appender-ref ref="RollingLogFileAppender" />-->
    <appender-ref ref="AdoNetAppender" />
  </root>  
</log4net>
4

1 回答 1

1

在当前配置中,buffersize 最初设置为 100。所以我必须将其设置为 0 或 1,以便 log4net 立即发送消息。

<bufferSize value="0" />

第二个问题是我以错误的方式使用命令参数,我已将其设置为

<commandText value="INSERT INTO system_log(thread_id,level,appname,message,action) VALUES (@threadParam,@levelParam,@appnameParam,@messageParam,@actionParam);" />

哪个有效

于 2015-04-09T09:43:43.700 回答