我尝试使用log4net记录错误消息并使用SQL将它们保存到数据库中,但根本没有任何事情发生....我错过了什么吗?这是我的代码
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
hierarchy.Root.RemoveAllAppenders(); /*Remove any other appenders*/
log4net.Repository.Hierarchy.Logger rootLogger = hierarchy.Root;
rootLogger.Level = hierarchy.LevelMap["ERROR"];
AdoNetAppender ado = new AdoNetAppender();
ado.ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
ado.ConnectionString = ConfigurationManager.AppSettings["MSSQLConnectionString_nvn1"];
ado.CommandText = "INSERT INTO Log ([Date],[Thread],[Level],[Identity],[Username],[Logger],[Message]) VALUES (@log_date, @thread , @level , @identity , @username , @logger, @message)";
ado.BufferSize = 1;
ado.Threshold = log4net.Core.Level.Error;
AdoNetAppenderParameter logd = new AdoNetAppenderParameter();
logd.ParameterName = "@log_date";
logd.DbType = DbType.DateTime;
logd.Layout = new log4net.Layout.RawTimeStampLayout();
ado.AddParameter(logd);
...........
ado.ActivateOptions();
log4net.Config.BasicConfigurator.Configure(ado);
感谢。
总结:有人可以查看我的代码并尝试给我一些关于如何将log4net中的错误消息保存到数据库中的建议。
答案 0 :(得分:0)
在配置文件或代码中将缓冲区大小设置为“1”,默认缓冲区大小为512:
<bufferSize value="1" />