Added serilog thread enricher and shrotened the context log story.

This commit is contained in:
Ruberoid 2025-07-22 19:39:40 +03:00
parent e7ec9b8aa6
commit 9316f08a73
5 changed files with 21 additions and 1 deletions

View File

@ -9,6 +9,7 @@
<PackageVersion Include="Nocr.TextMatcher.Async.Api.Contracts" Version="0.6.2" />
<PackageVersion Include="prometheus-net" Version="8.2.1" />
<PackageVersion Include="prometheus-net.AspNetCore" Version="8.2.1" />
<PackageVersion Include="Serilog.Enrichers.Thread" Version="4.0.0" />
</ItemGroup>
<ItemGroup Label="Telegram">
<PackageVersion Include="WTelegramClient" Version="4.3.4" />

View File

@ -18,6 +18,8 @@ public class HostBuilderFactory<TStartup> where TStartup : class
.UseSerilog((ctx, logBuilder) =>
{
logBuilder.ReadFrom.Configuration(ctx.Configuration)
.Enrich.With<ShortSourceContextEnricher>()
.Enrich.WithThreadId()
.Enrich.FromLogContext();
});

View File

@ -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);
}
}
}

View File

@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="prometheus-net.AspNetCore" />
<PackageReference Include="Serilog.AspNetCore" />
<PackageReference Include="Serilog.Enrichers.Thread" />
</ItemGroup>
<ItemGroup Label="Rebus">

View File

@ -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}"
}
}
]