private readonly string sql = ""; // 日志sql语句 public DbLogEventHandler(DbLogOptions dbLogOptions) { _dbLogOptions = dbLogOptions; if (_dbLogOptions.IsDbSharding) { switch (_dbLogOptions.DbShardingRule) { case 1: tableName = tableName + "_" + DbShardingHelper.DayRule(DateTime.Now); break; case 2: tableName = tableName + "_" + DbShardingHelper.MonthRule(DateTime.Now); break; } } sql = string.Format(@"INSERT INTO {0} (Id,AddTime,ClassName,ProjectName, LogTag, LogType, LogMessage, IP) VALUES (@Id,@AddTime,@ClassName,@ProjectName, @LogTag, @LogType, @LogMessage, @IP)" , tableName); }
public async Task <bool> HandleAsync(LogEvent @event, CancellationToken cancellationToken = default(CancellationToken)) { try { if (_dbLogOptions.IsWriteConsole) { // Console.WriteLine(JsonConvert.SerializeObject(@event)); return(true); } // 表名 var tableName = "tb_logs"; if (_dbLogOptions.IsDbSharding) { switch (_dbLogOptions.DbShardingRule) { case 1: tableName = tableName + "_" + DbShardingHelper.DayRule(DateTime.Now); break; case 2: tableName = tableName + "_" + DbShardingHelper.MonthRule(DateTime.Now); break; } } var model = @event.LogInfo; var sql = string.Format(@"INSERT INTO {0} (Id,AddTime,ClassName,ProjectName, LogTag, LogType, LogMessage, IP) VALUES (@Id,@AddTime,@ClassName,@ProjectName, @LogTag, @LogType, @LogMessage, @IP)" , tableName); using (var connection = new MySqlConnection(_dbLogOptions.ConnectionString)) { await connection.ExecuteAsync(sql, model); } } catch (Exception ex) { Console.WriteLine(ex.Message + ex.InnerException.Message + ex.StackTrace); } return(true); }