log4net使用ConsoleAppender记录得很好,但是将Null记录到MySql

时间:2018-08-28 08:47:36

标签: c# mysql logging odbc log4net

我被困在这里好几天了。我通过ODBC连接到Mysql,并尝试将所有内容记录到db。在使用Log4net的情况下,ConsoleAppender可以很好地工作,但是AdoNetAppenders不能,它将null记录到数据库,但是使用控制台,一切都非常完美。这是我的整个app.config。我没有看到任何错误。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" />
</configSections>
<appSettings>
  <add key="log4net.Internal.Debug" value="true" />
</appSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /></startup>
<log4net debug="true">
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">    
<layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="%date--%property{hostname}-%-4timestamp [%thread] %-5level %logger - %message%newline"/>
</layout>
</appender>

这是AdoNetAppender

<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
  <bufferSize value="1" />
  <connectionType value="System.Data.Odbc.OdbcConnection, System.Data,version=1.0.3300.0, publicKeyToken=b77a5c561934e089, culture=neutral" />
  <connectionString value="DSN=tmhe_sa2_local;" />
  <commandType value="Text"></commandType>
  <commandText value="INSERT INTO si_products.log (Date,Thread,Level,Logger,Message,Exception,HostName) VALUES (@date, @thread, @level, @logger, @message, @exception, @hostname);" />
  <parameter>
    <parameterName value="@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="@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>
  <parameter>
    <parameterName value="@hostname" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%property{hostname}" />
    </layout>
  </parameter>
</appender>
<root>
  <level value="ALL" />
  <appender-ref ref="AdoNetAppender" />
  <appender-ref ref="ConsoleAppender" />
</root>
</log4net>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Google.Protobuf" publicKeyToken="a7d26565bac4d604" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.6.1.0" newVersion="3.6.1.0" />
  </dependentAssembly>
</assemblyBinding>
</runtime>
<system.diagnostics>
<trace autoflush="true">
  <listeners>
    <add
        name="textWriterTraceListener"
        type="System.Diagnostics.TextWriterTraceListener"
        initializeData="C:\Users\tung\Desktop\programm\log4net.txt" />
  </listeners>
</trace>
</system.diagnostics>
</configuration>

`

0 个答案:

没有答案