From 101bb908bd0567f39ef6f2bc27fd39a2853c04a3 Mon Sep 17 00:00:00 2001 From: Anatolii Grynchuk Date: Sat, 2 May 2026 00:21:25 +0300 Subject: [PATCH] feat: add Serilog with Console and Seq sinks, log TransactionBehavior - Add Serilog.AspNetCore + Sinks.Console + Sinks.Seq to Api - Add Serilog.Extensions.Hosting + Sinks.Console + Sinks.Seq to Worker - Add Microsoft.Extensions.Logging.Abstractions to Services - TransactionBehavior logs handler name, elapsed time, and errors Ref: IT-628 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Directory.Packages.props | 7 ++++ .../HrynCo.NotificationService.Api.csproj | 3 ++ .../Behaviors/TransactionBehavior.cs | 33 +++++++++++++++---- ...HrynCo.NotificationService.Services.csproj | 1 + .../HrynCo.NotificationService.Worker.csproj | 3 ++ 5 files changed, 40 insertions(+), 7 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 4137eeb..dbdf273 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -15,8 +15,15 @@ + + + + + + + diff --git a/HrynCo.NotificationService.Api/HrynCo.NotificationService.Api.csproj b/HrynCo.NotificationService.Api/HrynCo.NotificationService.Api.csproj index ef39b79..537ed15 100644 --- a/HrynCo.NotificationService.Api/HrynCo.NotificationService.Api.csproj +++ b/HrynCo.NotificationService.Api/HrynCo.NotificationService.Api.csproj @@ -8,6 +8,9 @@ + + + diff --git a/HrynCo.NotificationService.Services/Behaviors/TransactionBehavior.cs b/HrynCo.NotificationService.Services/Behaviors/TransactionBehavior.cs index dcccbba..dfe96ac 100644 --- a/HrynCo.NotificationService.Services/Behaviors/TransactionBehavior.cs +++ b/HrynCo.NotificationService.Services/Behaviors/TransactionBehavior.cs @@ -1,5 +1,7 @@ +using System.Diagnostics; using HrynCo.NotificationService.DAL.Abstract; using MediatR; +using Microsoft.Extensions.Logging; namespace HrynCo.NotificationService.Services.Behaviors; @@ -7,19 +9,36 @@ public class TransactionBehavior : IPipelineBehavior> _logger; - public TransactionBehavior(IUnitOfWork unitOfWork) + public TransactionBehavior(IUnitOfWork unitOfWork, ILogger> logger) { _unitOfWork = unitOfWork; + _logger = logger; } - public Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) + public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) { - return _unitOfWork.ExecuteInTransactionAsync(async () => + string handlerName = typeof(TRequest).Name; + _logger.LogDebug("Handling {Handler}", handlerName); + + Stopwatch sw = Stopwatch.StartNew(); + try { - TResponse result = await next(); - await _unitOfWork.SaveChangesAsync(cancellationToken); + TResponse result = await _unitOfWork.ExecuteInTransactionAsync(async () => + { + TResponse response = await next(); + await _unitOfWork.SaveChangesAsync(cancellationToken); + return response; + }); + + _logger.LogDebug("Handled {Handler} in {ElapsedMs}ms", handlerName, sw.ElapsedMilliseconds); return result; - }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Handler {Handler} failed after {ElapsedMs}ms", handlerName, sw.ElapsedMilliseconds); + throw; + } } -} +} \ No newline at end of file diff --git a/HrynCo.NotificationService.Services/HrynCo.NotificationService.Services.csproj b/HrynCo.NotificationService.Services/HrynCo.NotificationService.Services.csproj index e7978bb..e7cba24 100644 --- a/HrynCo.NotificationService.Services/HrynCo.NotificationService.Services.csproj +++ b/HrynCo.NotificationService.Services/HrynCo.NotificationService.Services.csproj @@ -2,6 +2,7 @@ + diff --git a/HrynCo.NotificationService.Worker/HrynCo.NotificationService.Worker.csproj b/HrynCo.NotificationService.Worker/HrynCo.NotificationService.Worker.csproj index 8f82f22..b63bd32 100644 --- a/HrynCo.NotificationService.Worker/HrynCo.NotificationService.Worker.csproj +++ b/HrynCo.NotificationService.Worker/HrynCo.NotificationService.Worker.csproj @@ -9,6 +9,9 @@ + + +