public void Emit(LogEvent logEvent) { try { using (var conn = new MySqlConnection(_connectionString)) { conn.Open(); var columnValues = _core.GetColumnsAndValues(logEvent).ToList(); var commandText = _core.GetInsertStatement(columnValues); using (var cmd = new MySqlCommand(commandText, conn)) { foreach (var columnValue in columnValues) { if (columnValue.Value != null) { cmd.Parameters.AddWithValue(columnValue.Key, columnValue.Value); } } cmd.ExecuteNonQuery(); } } } catch (Exception ex) { SelfLog.WriteLine("Unable to write log event to the database due to following error: {1}", ex.Message); throw; } }
private async Task Insert(IEnumerable <LogEvent> events, MySqlConnection connection) { foreach (var log in events) { try { var columnValues = _core.GetColumnsAndValues(log).ToList(); var commandText = _core.GetInsertStatement(columnValues); using (var cmd = new MySqlCommand(commandText, connection)) { foreach (var columnValue in columnValues) { if (columnValue.Value != null) { cmd.Parameters.AddWithValue(columnValue.Key, columnValue.Value); } } await cmd.ExecuteNonQueryAsync().ConfigureAwait(false); } } catch (Exception ex) { SelfLog.WriteLine("Unable to write log event to the database due to following error: {0}", ex.Message); } } }
public void Emit(LogEvent logEvent) { try { using var conn = new MySqlConnection(ConnectionString); conn.Open(); var columnValues = Core.GetColumnsAndValues(logEvent).ToList(); using var cmd = new MySqlCommand(Core.GetInsertStatement(columnValues), conn); columnValues.Where(x => x.Value != null).ToList().ForEach(x => cmd.Parameters.AddWithValue(x.Key, x.Value)); cmd.ExecuteNonQuery(); } catch (Exception ex) { SelfLog.WriteLine("Unable to write log event to the database due to following error: {1}", ex.Message); throw; } }