Skip to content

Commit

Permalink
support UtcTimeStamp for serilog
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhu committed May 4, 2023
1 parent 74219d6 commit cec3d44
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 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
43 changes: 36 additions & 7 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 Down Expand Up @@ -80,13 +83,23 @@ 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>
{
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 }
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"},
};
columnOpts.LogEvent.DataLength = 2048;
columnOpts.PrimaryKey = columnOpts.Id;
Expand Down Expand Up @@ -147,4 +160,20 @@ private static void WriteToSqLite(LoggerConfiguration serilogConfig, string? con
LogEventLevel.Information
));
}


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


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

0 comments on commit cec3d44

Please sign in to comment.