Skip to content

Commit

Permalink
Small Code Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Bram1903 committed May 5, 2023
1 parent cec3d44 commit 8cc585e
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions src/Infrastructure/Extensions/SerilogExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ private static void WriteToDatabase(this LoggerConfiguration serilogConfig, ICon
string? dbProvider =
configuration.GetValue<string>($"{nameof(DatabaseSettings)}:{nameof(DatabaseSettings.DBProvider)}");
string? connectionString =
configuration.GetValue<string>(
$"{nameof(DatabaseSettings)}:{nameof(DatabaseSettings.ConnectionString)}");
configuration.GetValue<string>($"{nameof(DatabaseSettings)}:{nameof(DatabaseSettings.ConnectionString)}");
switch (dbProvider)
{
case DbProviderKeys.SqlServer:
Expand Down Expand Up @@ -83,25 +82,28 @@ private static void WriteToSqlServer(LoggerConfiguration serilogConfig, string?
BatchPeriod = new TimeSpan(0, 0, 20)
};

ColumnOptions columnOpts = new() {
Store = new Collection<StandardColumn> {
StandardColumn.Id,
StandardColumn.TimeStamp,
StandardColumn.Level,
StandardColumn.LogEvent,
StandardColumn.Exception,
StandardColumn.Message,
StandardColumn.MessageTemplate,
StandardColumn.Properties
},
AdditionalColumns= new Collection<SqlColumn> {
new() { ColumnName = "ClientIP", PropertyName = "ClientIp", DataType = SqlDbType.NVarChar, DataLength = 64 },
new() { ColumnName = "UserName", PropertyName = "UserName", DataType = SqlDbType.NVarChar, DataLength = 64 },
new() { ColumnName = "ClientAgent", PropertyName = "ClientAgent", DataType = SqlDbType.NVarChar, DataLength = -1 }
},
TimeStamp = { ConvertToUtc = true, ColumnName = "TimeStamp"},
ColumnOptions columnOpts = new()
{
Store = new Collection<StandardColumn>
{
StandardColumn.Id,
StandardColumn.TimeStamp,
StandardColumn.Level,
StandardColumn.LogEvent,
StandardColumn.Exception,
StandardColumn.Message,
StandardColumn.MessageTemplate,
StandardColumn.Properties
},
AdditionalColumns = new Collection<SqlColumn>
{
new() { ColumnName = "ClientIP", PropertyName = "ClientIp", DataType = SqlDbType.NVarChar, DataLength = 64 },
new() { ColumnName = "UserName", PropertyName = "UserName", DataType = SqlDbType.NVarChar, DataLength = 64 },
new() { ColumnName = "ClientAgent", PropertyName = "ClientAgent", DataType = SqlDbType.NVarChar, DataLength = -1 }
},
TimeStamp = { ConvertToUtc = true, ColumnName = "TimeStamp" },
LogEvent = { DataLength = 2048 }
};
columnOpts.LogEvent.DataLength = 2048;
columnOpts.PrimaryKey = columnOpts.Id;
columnOpts.TimeStamp.NonClusteredIndex = true;

Expand Down Expand Up @@ -131,7 +133,7 @@ private static void WriteToNpgsql(LoggerConfiguration serilogConfig, string? con
{ "Exception", new ExceptionColumnWriter(NpgsqlDbType.Text) },
{ "Properties", new PropertiesColumnWriter(NpgsqlDbType.Varchar) },
{ "LogEvent", new LogEventSerializedColumnWriter(NpgsqlDbType.Varchar) },
{ "UserName", new SinglePropertyColumnWriter("UserName", PropertyWriteMethod.Raw, NpgsqlDbType.Varchar) },
{ "UserName", new SinglePropertyColumnWriter("UserName", PropertyWriteMethod.Raw, NpgsqlDbType.Varchar) },
{ "ClientIP", new SinglePropertyColumnWriter("ClientIp", PropertyWriteMethod.Raw, NpgsqlDbType.Varchar) },
{ "ClientAgent", new SinglePropertyColumnWriter("ClientAgent", PropertyWriteMethod.ToString, NpgsqlDbType.Varchar) }
};
Expand Down Expand Up @@ -162,15 +164,13 @@ private static void WriteToSqLite(LoggerConfiguration serilogConfig, string? con
}


public static LoggerConfiguration WithUtcTime(
this LoggerEnrichmentConfiguration enrichmentConfiguration)
public static LoggerConfiguration WithUtcTime(this LoggerEnrichmentConfiguration enrichmentConfiguration)
{
return enrichmentConfiguration.With<UtcTimestampEnricher>();
}
}


class UtcTimestampEnricher : ILogEventEnricher
internal class UtcTimestampEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory pf)
{
Expand Down

0 comments on commit 8cc585e

Please sign in to comment.