From cec3d447ac415fc24b4543d871057ab40a9bd07d Mon Sep 17 00:00:00 2001 From: "hualin.zhu" Date: Fri, 5 May 2023 07:44:25 +0800 Subject: [PATCH] support UtcTimeStamp for serilog --- src/Domain/Entities/Logger/Logger.cs | 2 +- .../Extensions/SerilogExtensions.cs | 43 ++++++++++++++++--- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/Domain/Entities/Logger/Logger.cs b/src/Domain/Entities/Logger/Logger.cs index 5cbea3eb4..c50de10fc 100644 --- a/src/Domain/Entities/Logger/Logger.cs +++ b/src/Domain/Entities/Logger/Logger.cs @@ -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; } diff --git a/src/Infrastructure/Extensions/SerilogExtensions.cs b/src/Infrastructure/Extensions/SerilogExtensions.cs index b77e9c243..bff5ee520 100644 --- a/src/Infrastructure/Extensions/SerilogExtensions.cs +++ b/src/Infrastructure/Extensions/SerilogExtensions.cs @@ -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; @@ -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)) @@ -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 - { - 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.Id, + StandardColumn.TimeStamp, + StandardColumn.Level, + StandardColumn.LogEvent, + StandardColumn.Exception, + StandardColumn.Message, + StandardColumn.MessageTemplate, + StandardColumn.Properties + }, + AdditionalColumns= new Collection { + 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; @@ -147,4 +160,20 @@ private static void WriteToSqLite(LoggerConfiguration serilogConfig, string? con LogEventLevel.Information )); } + + + public static LoggerConfiguration WithUtcTime( + this LoggerEnrichmentConfiguration enrichmentConfiguration) + { + return enrichmentConfiguration.With(); + } +} + + +class UtcTimestampEnricher : ILogEventEnricher +{ + public void Enrich(LogEvent logEvent, ILogEventPropertyFactory pf) + { + logEvent.AddOrUpdateProperty(pf.CreateProperty("TimeStamp", logEvent.Timestamp.UtcDateTime)); + } } \ No newline at end of file