Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

c# - log4net logging error in mysql database logging

i am using log4net for logging in mysql database. log4net configuration is correct and because i have tested to log in sql server and in file but when i'm writing logs in mysql database then it prompt an error to me.

my log4net config is below

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>  
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>  
  </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
    </startup>

  <log4net>
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="1" />
      <connectionType value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data" />
      <connectionString value="Server=127.0.0.1;Database=logdb;Uid=abc;Pwd=abc;" />
      <commandText value="INSERT INTO log (timestamp,Thread,Level,Logger,Message) VALUES (?log_date, ?thread, ?log_level, ?logger, ?message);" />
      <parameter>
        <parameterName value="?log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
      <parameter>
        <parameterName value="?thread" />
        <dbType value="String" />
        <size value="32" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%thread" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="?log_level" />
        <dbType value="String" />
        <size value="32" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="?logger" />
        <dbType value="String" />
        <size value="256" />
        <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>
      
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="AdoNetAppender" />
    </root>
  </log4net>
  
  <appSettings>
    <add key="log4net.Internal.Debug" value="true"/>
  </appSettings>

<system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add name="textWriterTraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:Logslog4net.txt"/>
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

and the log consuming code is here

 class Program
        {
            private static readonly ILog Logger = LogManager.GetLogger(System.Environment.MachineName);
    
            static void Main(string[] args)
            {
                log4net.Config.XmlConfigurator.Configure();
    
                Logger.Info("Testing information log from .Net app");
                Logger.Debug("Testing Debug log from .Net app");
                Logger.Fatal("Testing Fatal log from .Net app");
            }
        }

i also the "[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)] " this line in assembly file. the error i'm encountering is

**

log4net:ERROR [AdoNetAppender] ErrorCode: GenericFailure. Exception while writing to database
System.InvalidOperationException: Parameter '?log_date' was not found during prepare.
   at MySql.Data.MySqlClient.PreparableStatement.Prepare()
   at MySql.Data.MySqlClient.MySqlCommand.Prepare(Int32 cursorPageSize)
   at MySql.Data.MySqlClient.MySqlCommand.Prepare()
   at log4net.Appender.AdoNetAppender.SendBuffer(IDbTransaction dbTran, LoggingEvent[] events)
   at log4net.Appender.AdoNetAppender.SendBuffer(LoggingEvent[] events)
log4net: Shutdown called on Hierarchy [log4net-default-repository]

**

but actually I'm passing all required parameters dnt know why I'm getting this error.

can anyone guide.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a bug in MySql.Data. There isn't a bug report filed for it as far as I know, but you can see from the MySqlConnector integration tests that MySql.Data fails if you call MySqlCommand.Prepare before adding the parameters.

However, that's exactly what AdoNetAppender does. (There is a try/catch block in that code, but that's a recent change, after 2.0.12.)

There are two possible fixes:

  1. Switch to MySqlConnector and change the <connectionType /> to use it.
  2. Add IgnorePrepare=true to your MySql.Data connection string, which will cause Prepare to be a no-op. (Note: this option was removed in 8.0.24.)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...