コード例 #1
0
        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;
            }
        }
コード例 #2
0
        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);
                }
            }
        }
コード例 #3
0
        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;
            }
        }