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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…