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 @@ + + +