Skip to content

Commit

Permalink
Merge branch 'master' of C:\Users\Majid\source\repos\DDDCommon with c…
Browse files Browse the repository at this point in the history
…onflicts.
  • Loading branch information
hatami57 committed Oct 1, 2019
1 parent 9921ef7 commit ce7fc1f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
19 changes: 19 additions & 0 deletions Utils/SerilogPostgreSqlSink/ColumnWriterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,25 @@ public override object GetValue(LogEvent logEvent, IFormatProvider formatProvide
return str.Substring(1, str.Length - 2);
}
}

/// <summary>
/// Writes App name if exists
/// </summary>
public class AppColumnWriter : ColumnWriterBase
{
public AppColumnWriter(NpgsqlDbType dbType = NpgsqlDbType.Varchar) : base(dbType)
{
}

public override object GetValue(LogEvent logEvent, IFormatProvider formatProvider = null)
{
if (!logEvent.Properties.TryGetValue("RequestId", out var value))
return null;

var str = value.ToString();
return str.Substring(1, str.Length - 2);
}
}

/// <summary>
/// Writes log event as json
Expand Down
33 changes: 18 additions & 15 deletions Utils/SerilogPostgreSqlSink/PostgreSqlSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,25 +132,28 @@ private string GetFullTableName(string tableName, string schemaName)

protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
{
using (var connection = new NpgsqlConnection(_connectionString))
{
await connection.OpenAsync();

if (!_isTableCreated)
try
{
using (var connection = new NpgsqlConnection(_connectionString))
{
TableCreator.CreateTable(connection, _fullTableName, _columnOptions);
_isTableCreated = true;
}
await connection.OpenAsync();

if (_useCopy)
{
ProcessEventsByCopyCommand(events, connection);
}
else
{
await ProcessEventsByInsertStatements(events, connection);
if (!_isTableCreated)
{
TableCreator.CreateTable(connection, _fullTableName, _columnOptions);
_isTableCreated = true;
}

if (_useCopy)
ProcessEventsByCopyCommand(events, connection);
else
await ProcessEventsByInsertStatements(events, connection);
}
}
catch (Exception e)
{
Log.Error(e, "could not insert logs to PostgreSQL database");
}
}

private async Task ProcessEventsByInsertStatements(IEnumerable<LogEvent> events, NpgsqlConnection connection)
Expand Down

0 comments on commit ce7fc1f

Please sign in to comment.