diff --git a/Build/Build.cs b/Build/Build.cs index d68db1fd..aebee429 100644 --- a/Build/Build.cs +++ b/Build/Build.cs @@ -135,7 +135,7 @@ protected override void OnBuildInitialized() { Log.Information("Generating NuGet packages for projects in solution"); int commitNum = 0; - string NuGetVersionCustom = "2.0.0.871"; + string NuGetVersionCustom = "2.0.0.872"; //if it's not a tagged release - append the commit number to the package version diff --git a/Src/RCommon.Mediatr/Behaviors/LoggingBehavior.cs b/Src/RCommon.Mediatr/Behaviors/LoggingBehavior.cs index df584f08..19d87c6a 100644 --- a/Src/RCommon.Mediatr/Behaviors/LoggingBehavior.cs +++ b/Src/RCommon.Mediatr/Behaviors/LoggingBehavior.cs @@ -10,11 +10,28 @@ namespace RCommon.Mediator.MediatR.Behaviors { - public class LoggingBehavior : IPipelineBehavior + public class LoggingRequestBehavior : IPipelineBehavior + where TRequest : IRequest + { + private readonly ILogger> _logger; + public LoggingRequestBehavior(ILogger> logger) => _logger = logger; + + + public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) + { + _logger.LogInformation("----- Handling command {CommandName} ({@Command})", request.GetGenericTypeName(), request); + var response = await next(); + _logger.LogInformation("----- Command {CommandName} handled - response: {@Response}", request.GetGenericTypeName(), response); + + return response; + } + } + + public class LoggingRequestWithResponseBehavior : IPipelineBehavior where TRequest : IRequest { - private readonly ILogger> _logger; - public LoggingBehavior(ILogger> logger) => _logger = logger; + private readonly ILogger> _logger; + public LoggingRequestWithResponseBehavior(ILogger> logger) => _logger = logger; public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) diff --git a/Src/RCommon.Mediatr/Behaviors/UnitOfWorkBehavior.cs b/Src/RCommon.Mediatr/Behaviors/UnitOfWorkBehavior.cs index a33a709f..1a1d4f55 100644 --- a/Src/RCommon.Mediatr/Behaviors/UnitOfWorkBehavior.cs +++ b/Src/RCommon.Mediatr/Behaviors/UnitOfWorkBehavior.cs @@ -8,15 +8,15 @@ namespace RCommon.Mediator.MediatR.Behaviors { - public class UnitOfWorkBehavior : IPipelineBehavior - where TRequest : IRequest + public class UnitOfWorkRequestBehavior : IPipelineBehavior + where TRequest : IRequest { - private readonly ILogger> _logger; + private readonly ILogger> _logger; private readonly IUnitOfWorkFactory _unitOfWorkScopeFactory; private readonly IUnitOfWorkManager _unitOfWorkManager; - public UnitOfWorkBehavior(IUnitOfWorkFactory unitOfWorkScopeFactory, IUnitOfWorkManager unitOfWorkManager, - ILogger> logger) + public UnitOfWorkRequestBehavior(IUnitOfWorkFactory unitOfWorkScopeFactory, IUnitOfWorkManager unitOfWorkManager, + ILogger> logger) { _unitOfWorkScopeFactory = unitOfWorkScopeFactory ?? throw new ArgumentException(nameof(IUnitOfWorkFactory)); _unitOfWorkManager = unitOfWorkManager ?? throw new ArgumentException(nameof(IUnitOfWorkManager)); @@ -44,6 +44,53 @@ public async Task Handle(TRequest request, RequestHandlerDelegate : IPipelineBehavior + where TRequest : IRequest + { + private readonly ILogger> _logger; + private readonly IUnitOfWorkFactory _unitOfWorkScopeFactory; + private readonly IUnitOfWorkManager _unitOfWorkManager; + + public UnitOfWorkRequestWithResponseBehavior(IUnitOfWorkFactory unitOfWorkScopeFactory, IUnitOfWorkManager unitOfWorkManager, + ILogger> logger) + { + _unitOfWorkScopeFactory = unitOfWorkScopeFactory ?? throw new ArgumentException(nameof(IUnitOfWorkFactory)); + _unitOfWorkManager = unitOfWorkManager ?? throw new ArgumentException(nameof(IUnitOfWorkManager)); + _logger = logger ?? throw new ArgumentException(nameof(ILogger)); + } + + public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) + { + var response = default(TResponse); + var typeName = request.GetGenericTypeName(); + + try + { + using (var unitOfWork = this._unitOfWorkScopeFactory.Create(TransactionMode.Default)) + { + _logger.LogInformation("----- Begin transaction {UnitOfWorkTransactionId} for {CommandName} ({@Command})", + this._unitOfWorkManager.CurrentUnitOfWork.TransactionId, typeName, request); + + response = await next(); + + _logger.LogInformation("----- Commit transaction {UnitOfWorkTransactionId} for {CommandName}", + this._unitOfWorkManager.CurrentUnitOfWork.TransactionId, typeName); + + unitOfWork.Commit(); + } + + return response; } catch (Exception ex) diff --git a/Src/RCommon.Mediatr/MediatRBuilderExtensions.cs b/Src/RCommon.Mediatr/MediatRBuilderExtensions.cs index 7aee7281..5862fca6 100644 --- a/Src/RCommon.Mediatr/MediatRBuilderExtensions.cs +++ b/Src/RCommon.Mediatr/MediatRBuilderExtensions.cs @@ -53,7 +53,8 @@ public static void AddRequest(this IMediatRB public static void AddLoggingToRequestPipeline(this IMediatRBuilder builder) { - builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>)); + builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(LoggingRequestBehavior<,>)); + builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(LoggingRequestWithResponseBehavior<,>)); } public static void AddValidationToRequestPipeline(this IMediatRBuilder builder) @@ -64,7 +65,8 @@ public static void AddValidationToRequestPipeline(this IMediatRBuilder builder) public static void AddUnitOfWorkToRequestPipeline(this IMediatRBuilder builder) { - builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(UnitOfWorkBehavior<,>)); + builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(UnitOfWorkRequestBehavior<,>)); + builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(UnitOfWorkRequestWithResponseBehavior<,>)); } }