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
@@ -3,6 +3,7 @@
<ItemGroup>
<PackageReference Include="MediatR" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Serilog" />
</ItemGroup>
<ItemGroup>
@@ -0,0 +1,8 @@
using Serilog;
namespace HrynCo.NotificationService.Services.Logging;
public sealed class ContextualSerilogLogger<TContext> : IContextualSerilogLogger<TContext>
{
public ILogger Logger { get; } = Log.ForContext<TContext>();
}
@@ -0,0 +1,11 @@
using System.Diagnostics.CodeAnalysis;
using Serilog;
namespace HrynCo.NotificationService.Services.Logging;
[SuppressMessage("Major Code Smell", "S2326:Unused type parameters should be removed",
Justification = "Generic parameter used in implementation via ForContext<T>.")]
public interface IContextualSerilogLogger<TContext>
{
ILogger Logger { get; }
}
@@ -1,4 +1,5 @@
using HrynCo.NotificationService.Services.Behaviors;
using HrynCo.NotificationService.Services.Logging;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
@@ -14,6 +15,8 @@ public static class ServiceCollectionExtensions
cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(TransactionBehavior<,>));
});
services.AddTransient(typeof(IContextualSerilogLogger<>), typeof(ContextualSerilogLogger<>));
return services;
}
}