From 9316f08a73f0844fa2ed19314c8199391136af99 Mon Sep 17 00:00:00 2001 From: Ruberoid Date: Tue, 22 Jul 2025 19:39:40 +0300 Subject: [PATCH] Added serilog thread enricher and shrotened the context log story. --- Directory.Packages.props | 1 + .../Infrastructure/HostBuilderFactory.cs | 2 ++ .../Infrastructure/ShortSourceContextEnricher.cs | 16 ++++++++++++++++ .../Nocr.TelegramListener.Host.csproj | 1 + src/Nocr.TelegramListener.Host/appsettings.json | 2 +- 5 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/Nocr.TelegramListener.Host/Infrastructure/ShortSourceContextEnricher.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index 3e160e1..1e656e6 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -9,6 +9,7 @@ + diff --git a/src/Nocr.TelegramListener.Host/Infrastructure/HostBuilderFactory.cs b/src/Nocr.TelegramListener.Host/Infrastructure/HostBuilderFactory.cs index adc35a7..33ee306 100644 --- a/src/Nocr.TelegramListener.Host/Infrastructure/HostBuilderFactory.cs +++ b/src/Nocr.TelegramListener.Host/Infrastructure/HostBuilderFactory.cs @@ -18,6 +18,8 @@ public class HostBuilderFactory where TStartup : class .UseSerilog((ctx, logBuilder) => { logBuilder.ReadFrom.Configuration(ctx.Configuration) + .Enrich.With() + .Enrich.WithThreadId() .Enrich.FromLogContext(); }); diff --git a/src/Nocr.TelegramListener.Host/Infrastructure/ShortSourceContextEnricher.cs b/src/Nocr.TelegramListener.Host/Infrastructure/ShortSourceContextEnricher.cs new file mode 100644 index 0000000..45254ad --- /dev/null +++ b/src/Nocr.TelegramListener.Host/Infrastructure/ShortSourceContextEnricher.cs @@ -0,0 +1,16 @@ +using Serilog.Core; +using Serilog.Events; + +public class ShortSourceContextEnricher : ILogEventEnricher +{ + public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) + { + if (logEvent.Properties.TryGetValue("SourceContext", out var sourceContextValue)) + { + var fullName = sourceContextValue.ToString().Trim('"'); // remove quotes + var shortName = fullName.Split('.').Last(); + var shortContext = propertyFactory.CreateProperty("ShortSourceContext", shortName); + logEvent.AddPropertyIfAbsent(shortContext); + } + } +} diff --git a/src/Nocr.TelegramListener.Host/Nocr.TelegramListener.Host.csproj b/src/Nocr.TelegramListener.Host/Nocr.TelegramListener.Host.csproj index 9558aa2..7f6bb28 100644 --- a/src/Nocr.TelegramListener.Host/Nocr.TelegramListener.Host.csproj +++ b/src/Nocr.TelegramListener.Host/Nocr.TelegramListener.Host.csproj @@ -8,6 +8,7 @@ + diff --git a/src/Nocr.TelegramListener.Host/appsettings.json b/src/Nocr.TelegramListener.Host/appsettings.json index a06c301..36638cd 100644 --- a/src/Nocr.TelegramListener.Host/appsettings.json +++ b/src/Nocr.TelegramListener.Host/appsettings.json @@ -12,7 +12,7 @@ { "Name": "Console", "Args": { - "outputTemplate": "[{Level:u3}] {Timestamp:MM-dd HH:mm:ss} {TraceId} {SourceContext:l} {Message:lj}{NewLine}{Exception}" + "outputTemplate": "[{Level:u3}] {Timestamp:MM-dd HH:mm:ss} [#{ThreadId}] {ShortSourceContext:l} {Message:lj}{NewLine}{Exception}" } } ]