コード例 #1
0
        public void WriteEvent(LogEvent logEvent)
        {
            try
            {
                using (var connection = _sqlConnectionFactory.Create())
                {
                    connection.Open();
                    using (SqlCommand command = connection.CreateCommand())
                    {
                        command.CommandType = CommandType.Text;

                        var fieldList     = new StringBuilder($"INSERT INTO [{_schemaName}].[{_tableName}] (");
                        var parameterList = new StringBuilder(") VALUES (");

                        var index = 0;
                        foreach (var field in _logEventDataGenerator.GetColumnsAndValues(logEvent))
                        {
                            if (index != 0)
                            {
                                fieldList.Append(',');
                                parameterList.Append(',');
                            }

                            fieldList.Append(field.Key);
                            parameterList.Append("@P");
                            parameterList.Append(index);

                            var parameter = new SqlParameter($"@P{index}", field.Value ?? DBNull.Value);

                            // The default is SqlDbType.DateTime, which will truncate the DateTime value if the actual
                            // type in the database table is datetime2. So we explicitly set it to DateTime2, which will
                            // work both if the field in the table is datetime and datetime2, which is also consistent with
                            // the behavior of the non-audit sink.
                            if (field.Value is DateTime)
                            {
                                parameter.SqlDbType = SqlDbType.DateTime2;
                            }

                            command.Parameters.Add(parameter);

                            index++;
                        }

                        parameterList.Append(')');
                        fieldList.Append(parameterList.ToString());

                        command.CommandText = fieldList.ToString();

                        command.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                SelfLog.WriteLine("Unable to write log event to the database due to following error: {1}", ex.Message);
                throw;
            }
        }
コード例 #2
0
        public void WriteEvent(LogEvent logEvent)
        {
            try
            {
                using (var connection = _sqlConnectionFactory.Create())
                {
                    connection.Open();
                    using (var command = connection.CreateCommand())
                    {
                        command.CommandType = CommandType.Text;

                        var fieldList     = new StringBuilder($"INSERT INTO [{_schemaName}].[{_tableName}] (");
                        var parameterList = new StringBuilder(") VALUES (");

                        var index = 0;
                        foreach (var field in _logEventDataGenerator.GetColumnsAndValues(logEvent))
                        {
                            if (index != 0)
                            {
                                fieldList.Append(',');
                                parameterList.Append(',');
                            }

                            fieldList.Append(field.Key);
                            parameterList.Append("@P");
                            parameterList.Append(index);

                            command.AddParameter($"@P{index}", field.Value);

                            index++;
                        }

                        parameterList.Append(')');
                        fieldList.Append(parameterList.ToString());

                        command.CommandText = fieldList.ToString();

                        command.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                SelfLog.WriteLine("Unable to write log event to the database due to following error: {1}", ex.Message);
                throw;
            }
        }
コード例 #3
0
        private void FillDataTable(IEnumerable <LogEvent> events, DataTable dataTable)
        {
            // Add the new rows to the collection.
            foreach (var logEvent in events)
            {
                var row = dataTable.NewRow();

                foreach (var field in _logEventDataGenerator.GetColumnsAndValues(logEvent))
                {
                    row[field.Key] = field.Value;
                }

                dataTable.Rows.Add(row);
            }

            dataTable.AcceptChanges();
        }