feat: replace manual Stopwatch with IProfiler in TransactionBehavior
- Add HrynCo.Common to Services project - TransactionBehavior now uses IProfiler.MeasureExecutionAsync: MeasureExecutionAsync -> ExecuteInTransactionAsync -> next() -> SaveChangesAsync - Profiler logs Start/End with duration + memory delta via Serilog PerformanceLog context - Register IProfiler as singleton in ServiceCollectionExtensions (uses Log.Logger) Ref: IT-628 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
using System.Diagnostics;
|
||||
using HrynCo.Common;
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.Behaviors;
|
||||
|
||||
@@ -9,36 +8,21 @@ public class TransactionBehavior<TRequest, TResponse> : IPipelineBehavior<TReque
|
||||
where TRequest : notnull
|
||||
{
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly ILogger<TransactionBehavior<TRequest, TResponse>> _logger;
|
||||
private readonly IProfiler _profiler;
|
||||
|
||||
public TransactionBehavior(IUnitOfWork unitOfWork, ILogger<TransactionBehavior<TRequest, TResponse>> logger)
|
||||
public TransactionBehavior(IUnitOfWork unitOfWork, IProfiler profiler)
|
||||
{
|
||||
_unitOfWork = unitOfWork;
|
||||
_logger = logger;
|
||||
_profiler = profiler;
|
||||
}
|
||||
|
||||
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
|
||||
{
|
||||
string handlerName = typeof(TRequest).Name;
|
||||
_logger.LogDebug("Handling {Handler}", handlerName);
|
||||
|
||||
Stopwatch sw = Stopwatch.StartNew();
|
||||
try
|
||||
{
|
||||
TResponse result = await _unitOfWork.ExecuteInTransactionAsync(async () =>
|
||||
public Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken) =>
|
||||
_profiler.MeasureExecutionAsync(
|
||||
() => _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;
|
||||
}
|
||||
}
|
||||
}),
|
||||
typeof(TRequest).Name);
|
||||
}
|
||||
@@ -17,8 +17,10 @@ public abstract class RequestHandler<TRequest, TResponse> : IRequestHandler<TReq
|
||||
protected ILogger Logger { get; }
|
||||
protected IUnitOfWork UnitOfWork { get; }
|
||||
|
||||
public Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken) =>
|
||||
DoOnHandle(request, cancellationToken);
|
||||
public Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
return DoOnHandle(request, cancellationToken);
|
||||
}
|
||||
|
||||
protected abstract Task<TResponse> DoOnHandle(TRequest request, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="HrynCo.Common" />
|
||||
<PackageReference Include="MediatR" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
|
||||
<PackageReference Include="Serilog" />
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
using HrynCo.Common;
|
||||
using HrynCo.NotificationService.Services.Behaviors;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Serilog;
|
||||
|
||||
namespace HrynCo.NotificationService.Services;
|
||||
|
||||
@@ -16,6 +18,7 @@ public static class ServiceCollectionExtensions
|
||||
});
|
||||
|
||||
services.AddTransient(typeof(IContextualSerilogLogger<>), typeof(ContextualSerilogLogger<>));
|
||||
services.AddSingleton<IProfiler>(new Profiler(Log.Logger));
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user