/// <inheritdoc cref="PeriodicBatchingSink" />
        /// <summary>
        ///     Emit a batch of log events, running to completion synchronously.
        /// </summary>
        /// <param name="events">The events to emit.</param>
        /// <remarks>
        ///     Override either
        ///     <see
        ///         cref="M:Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.EmitBatch(System.Collections.Generic.IEnumerable{Serilog.Events.LogEvent})" />
        ///     or
        ///     <see
        ///         cref="M:Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.EmitBatchAsync(System.Collections.Generic.IEnumerable{Serilog.Events.LogEvent})" />
        ///     ,
        ///     not both.
        /// </remarks>
        protected override void EmitBatch(IEnumerable <LogEvent> events)
        {
            using (var connection = new NpgsqlConnection(this.connectionString))
            {
                connection.Open();

                if (!this.isSchemaCreated && !string.IsNullOrWhiteSpace(this.schemaName))
                {
                    SchemaCreator.CreateSchema(connection, this.schemaName);
                    this.isSchemaCreated = true;
                }

                if (!this.isTableCreated)
                {
                    TableCreator.CreateTable(connection, this.schemaName, this.tableName, this.columnOptions);
                    this.isTableCreated = true;
                }

                if (this.useCopy)
                {
                    this.ProcessEventsByCopyCommand(events, connection);
                }
                else
                {
                    this.ProcessEventsByInsertStatements(events, connection);
                }
            }
        }
Esempio n. 2
0
        /// <inheritdoc cref="PeriodicBatchingSink" />
        /// <summary>
        ///     Emit a batch of log events, running to completion synchronously.
        /// </summary>
        /// <param name="events">The events to emit.</param>
        /// <remarks>
        ///     Override either
        ///     <see
        ///         cref="M:Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.EmitBatch(System.Collections.Generic.IEnumerable{Serilog.Events.LogEvent})" />
        ///     or
        ///     <see
        ///         cref="M:Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.EmitBatchAsync(System.Collections.Generic.IEnumerable{Serilog.Events.LogEvent})" />
        ///     ,
        ///     not both.
        /// </remarks>
        protected override void EmitBatch(IEnumerable <LogEvent> events)
        {
            try
            {
                using var connection = new NpgsqlConnection(this.connectionString);
                connection.Open();

                if (!this.isSchemaCreated && !string.IsNullOrWhiteSpace(this.schemaName))
                {
                    SchemaCreator.CreateSchema(connection, this.schemaName);
                    this.isSchemaCreated = true;
                }

                if (!this.isTableCreated)
                {
                    TableCreator.CreateTable(connection, this.schemaName, this.tableName, this.columnOptions);
                    this.isTableCreated = true;
                }

                if (this.useCopy)
                {
                    this.ProcessEventsByCopyCommand(events, connection);
                }
                else
                {
                    this.ProcessEventsByInsertStatements(events, connection);
                }
            }
            catch (Exception ex)
            {
                SelfLog.WriteLine($"{ex.Message} {ex.StackTrace}");
                this.failureCallback?.Invoke(ex);
            }
        }
Esempio n. 3
0
        protected override void EmitBatch(IEnumerable <LogEvent> events)
        {
            using (var connection = new NpgsqlConnection(_connectionString))
            {
                connection.Open();

                if (!_isTableCreated)
                {
                    TableCreator.CreateTable(connection, _fullTableName, _columnOptions);
                    _isTableCreated = true;
                }

                if (_useCopy)
                {
                    ProcessEventsByCopyCommand(events, connection);
                }
                else
                {
                    ProcessEventsByInsertStatements(events, connection);
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Emits the events.
        /// </summary>
        /// <param name="events">The events.</param>
        public void Emit(IEnumerable <LogEvent> events)
        {
            using var connection = new NpgsqlConnection(this.SinkOptions.ConnectionString);
            connection.Open();

            if (this.SinkOptions.NeedAutoCreateSchema && !this.isSchemaCreated && !string.IsNullOrWhiteSpace(this.SinkOptions.SchemaName))
            {
                SchemaCreator.CreateSchema(connection, this.SinkOptions.SchemaName);
                this.isSchemaCreated = true;
            }

            if (this.SinkOptions.NeedAutoCreateTable && !this.isTableCreated && !string.IsNullOrWhiteSpace(this.SinkOptions.TableName))
            {
                if (this.SinkOptions.ColumnOptions.All(c => c.Value.Order != null))
                {
                    var columnOptions = this.SinkOptions.ColumnOptions.OrderBy(c => c.Value.Order)
                                        .ToDictionary(c => c.Key, x => x.Value);
                    TableCreator.CreateTable(connection, this.SinkOptions.SchemaName, this.SinkOptions.TableName, columnOptions);
                }
                else
                {
                    TableCreator.CreateTable(connection, this.SinkOptions.SchemaName, this.SinkOptions.TableName, this.SinkOptions.ColumnOptions);
                }

                this.isTableCreated = true;
            }

            if (this.SinkOptions.UseCopy)
            {
                this.ProcessEventsByCopyCommand(events, connection);
            }
            else
            {
                this.ProcessEventsByInsertStatements(events, connection);
            }
        }