Skip to content

Commit

Permalink
Merge pull request #379 from neozhu/UtcTimestampEnricher
Browse files Browse the repository at this point in the history
Support UtcTimestamp for serilog
  • Loading branch information
Bram1903 authored May 5, 2023
2 parents 74219d6 + 8cc585e commit aba62d7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Domain/Entities/Logger/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class Logger : IEntity
public string? MessageTemplate { get; set; }
public string Level { get; set; } = default!;

public DateTime TimeStamp { get; set; } = DateTime.Now;
public DateTime TimeStamp { get; set; } = DateTime.UtcNow;
public string? Exception { get; set; }
public string? UserName { get; set; }
public string? ClientIP { get; set; }
Expand Down
49 changes: 39 additions & 10 deletions src/Infrastructure/Extensions/SerilogExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Microsoft.Extensions.Configuration;
using NpgsqlTypes;
using Serilog;
using Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;
using Serilog.Sinks.MSSqlServer;
using Serilog.Sinks.PostgreSQL;
Expand All @@ -29,6 +31,7 @@ public static void RegisterSerilog(this WebApplicationBuilder builder)
.MinimumLevel.Override("Hangfire.Server.ServerHeartbeatProcess", LogEventLevel.Error)
.MinimumLevel.Override("Hangfire.Processing.BackgroundExecution", LogEventLevel.Error)
.Enrich.FromLogContext()
.Enrich.WithUtcTime()
.Enrich.WithClientIp()
.Enrich.WithClientAgent()
.WriteTo.Async(wt => wt.File("./log/log-.txt", rollingInterval: RollingInterval.Day))
Expand All @@ -47,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 @@ -80,15 +82,28 @@ private static void WriteToSqlServer(LoggerConfiguration serilogConfig, string?
BatchPeriod = new TimeSpan(0, 0, 20)
};

ColumnOptions columnOpts = new();
columnOpts.Store.Add(StandardColumn.LogEvent);
columnOpts.AdditionalColumns = new Collection<SqlColumn>
ColumnOptions columnOpts = new()
{
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 }
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 @@ -118,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 @@ -147,4 +162,18 @@ private static void WriteToSqLite(LoggerConfiguration serilogConfig, string? con
LogEventLevel.Information
));
}


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

internal class UtcTimestampEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory pf)
{
logEvent.AddOrUpdateProperty(pf.CreateProperty("TimeStamp", logEvent.Timestamp.UtcDateTime));
}
}

0 comments on commit aba62d7

Please sign in to comment.