feat: add SerilogRegistrar and ContextualSerilogLogger

- IContextualSerilogLogger<T> / ContextualSerilogLogger<T> in Services/Logging
  (handlers get a ForContext<T>-scoped logger via DI, consistent with ItemTracker)
- SerilogRegistrar extension on WebApplicationBuilder (Api)
- SerilogRegistrar extension on HostApplicationBuilder (Worker)
- Both registrars: set Log.Logger, wire Logging + Host/Services, register ILogger singleton
- ServiceCollectionExtensions: register IContextualSerilogLogger<> as transient
- Program.cs in both apps simplified to single builder.AddSerilog() call

Ref: IT-628

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Anatolii Grynchuk
2026-05-02 00:38:32 +03:00
parent 4ea57b2068
commit a9bea183c1
9 changed files with 67 additions and 6 deletions
+1 -3
View File
@@ -1,12 +1,10 @@
using HrynCo.NotificationService.Api;
using HrynCo.NotificationService.DAL.EF;
using HrynCo.NotificationService.Services;
using Serilog;
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog((context, lc) =>
lc.ReadFrom.Configuration(context.Configuration));
builder.AddSerilog();
var appSettings = builder.Configuration
.GetSection(AppSettings.SectionName)
@@ -0,0 +1,20 @@
using Serilog;
namespace HrynCo.NotificationService.Api;
public static class SerilogRegistrar
{
public static void AddSerilog(this WebApplicationBuilder builder)
{
var loggerConfiguration = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Configuration)
.Enrich.FromLogContext();
Log.Logger = loggerConfiguration.CreateLogger();
builder.Logging.AddSerilog(Log.Logger);
builder.Host.UseSerilog();
builder.Services.AddSingleton(Log.Logger);
}
}