代码配置 Log4Net 的 AdoNetAppender
今天发现 Log4Net 输出到不同数据库时,其 commandtext 使用的语法居然也不同,摸索了好一会,分享一下:
// SqlServer // private const string SqlServerLog4NetCommandText = "INSERT INTO pacs_log ([Datetime],[ThreadID],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)"; private const string SqlServerConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; private static string SqlServerConnectionString(string dbHost, string dbName, string dbUser, string dbPwd) { var sb = new StringBuilder(); sb.AppendFormat("data source={0}", dbHost); sb.AppendFormat(";initial catalog={0}", dbName); sb.AppendFormat(";User ID={0};Password={1}", dbUser, dbPwd); sb.Append(";integrated security=false;persist security info=True;"); return sb.ToString(); } // MySql // private const string MySqlLog4NetCommandText = "INSERT INTO pacs_log (Datetime,ThreadID,Level,Logger,Message,Exception) VALUES (?log_date, ?thread, ?log_level, ?logger, ?message, ?exception)"; private const string MySqlConnectionType = "MySql.Data.MySqlClient.MySqlConnection, MySql.Data, Version=6.4.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"; private static string MySqlConnectionString(string dbHost, string dbName, string dbUser, string dbPwd) { var sb = new StringBuilder(); sb.AppendFormat("data source={0}", dbHost); sb.AppendFormat(";initial catalog={0}", dbName); sb.AppendFormat(";User ID={0};Password={1};persist security info=True;", dbUser, dbPwd); return sb.ToString(); }