From c8e20e9145dd4a95bb95c9cfa031b3ba4e662051 Mon Sep 17 00:00:00 2001 From: jasonmwebb-lv <97196139+jasonmwebb-lv@users.noreply.github.com> Date: Fri, 19 Apr 2024 00:34:56 -0600 Subject: [PATCH 1/3] Dev (#128) * Minor fix to build project. * Version bump. --- Build/Build.cs | 2 +- Build/Build.csproj | 25 ------------ .../Behaviors/ValidatorBehavior.cs | 38 ++++++++++++++++++- .../MediatRBuilderExtensions.cs | 1 + 4 files changed, 39 insertions(+), 27 deletions(-) diff --git a/Build/Build.cs b/Build/Build.cs index 389a67c0..26646579 100644 --- a/Build/Build.cs +++ b/Build/Build.cs @@ -134,7 +134,7 @@ protected override void OnBuildInitialized() { Log.Information("Generating NuGet packages for projects in solution"); int commitNum = 0; - string NuGetVersionCustom = "2.0.0.8"; + string NuGetVersionCustom = "2.0.0.10"; //if it's not a tagged release - append the commit number to the package version diff --git a/Build/Build.csproj b/Build/Build.csproj index 1bd56474..22b49174 100644 --- a/Build/Build.csproj +++ b/Build/Build.csproj @@ -21,29 +21,4 @@ - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/Src/RCommon.Mediatr/Behaviors/ValidatorBehavior.cs b/Src/RCommon.Mediatr/Behaviors/ValidatorBehavior.cs index 15c3ad3c..059ba76f 100644 --- a/Src/RCommon.Mediatr/Behaviors/ValidatorBehavior.cs +++ b/Src/RCommon.Mediatr/Behaviors/ValidatorBehavior.cs @@ -1,6 +1,7 @@ using FluentValidation; using MediatR; using Microsoft.Extensions.Logging; +using RCommon.Mediator.Subscribers; using System; using System.Collections.Generic; using System.Linq; @@ -10,8 +11,42 @@ namespace RCommon.Mediator.MediatR.Behaviors { - public class ValidatorBehavior : IPipelineBehavior + public class ValidatorBehaviorForMediatR : IPipelineBehavior where TRequest : IRequest + { + private readonly ILogger> _logger; + private readonly IEnumerable> _validators; + + public ValidatorBehaviorForMediatR(IEnumerable> validators, ILogger> logger) + { + _validators = validators ?? throw new ArgumentNullException(nameof(validators)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + } + + public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) + { + var typeName = request.GetGenericTypeName(); + + _logger.LogInformation("----- Validating command {CommandType}", typeName); + + if (_validators.Any()) + { + var context = new ValidationContext(request); + var validationResults = await Task.WhenAll(_validators.Select(v => v.ValidateAsync(context, cancellationToken))); + var failures = validationResults.SelectMany(r => r.Errors).Where(f => f != null).ToList(); + if (failures.Count != 0) + { + _logger.LogWarning("Validation errors - {CommandType} - Command: {@Command} - Errors: {@ValidationErrors}", typeName, request, failures); + string message = $"Command Validation Errors for type {typeof(TRequest).Name}"; + throw new FluentValidation.ValidationException(message, failures); + } + } + return await next(); + } + } + + public class ValidatorBehavior : IPipelineBehavior + where TRequest : IAppRequest { private readonly ILogger> _logger; private readonly IEnumerable> _validators; @@ -43,4 +78,5 @@ public async Task Handle(TRequest request, RequestHandlerDelegate), typeof(ValidatorBehavior<,>)); + builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(ValidatorBehaviorForMediatR<,>)); } public static void AddUnitOfWorkToRequestPipeline(this IMediatRBuilder builder) From 60cdbd6d75df54362e70daa6b115b19036a8e648 Mon Sep 17 00:00:00 2001 From: Jason Webb Date: Fri, 19 Apr 2024 00:39:41 -0600 Subject: [PATCH 2/3] Version bump. --- Build/Build.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Build/Build.cs b/Build/Build.cs index 26646579..a408375b 100644 --- a/Build/Build.cs +++ b/Build/Build.cs @@ -134,7 +134,7 @@ protected override void OnBuildInitialized() { Log.Information("Generating NuGet packages for projects in solution"); int commitNum = 0; - string NuGetVersionCustom = "2.0.0.10"; + string NuGetVersionCustom = "2.0.0.868"; //if it's not a tagged release - append the commit number to the package version From 9a624daec047901c6c3a6cd2ed7dd1a8f651e251 Mon Sep 17 00:00:00 2001 From: Jason Webb Date: Fri, 19 Apr 2024 12:08:44 -0600 Subject: [PATCH 3/3] Hardening logging related event handling. --- Build/Build.cs | 2 +- .../InMemoryTransactionalEventRouter.cs | 1 + .../PublishWithEventBusEventProducer.cs | 11 +++++++--- .../PublishWithMassTransitEventProducer.cs | 10 +++++++--- .../SendWithMassTransitEventProducer.cs | 20 +++++++++++++------ .../PublishWithMediatREventProducer.cs | 8 ++++++-- .../Producers/SendWithMediatREventProducer.cs | 8 ++++++-- .../PublishWithWolverineEventProducer.cs | 12 ++++++++--- .../SendWithWolverineEventProducer.cs | 11 +++++++--- 9 files changed, 60 insertions(+), 23 deletions(-) diff --git a/Build/Build.cs b/Build/Build.cs index a408375b..5f9bdce8 100644 --- a/Build/Build.cs +++ b/Build/Build.cs @@ -134,7 +134,7 @@ protected override void OnBuildInitialized() { Log.Information("Generating NuGet packages for projects in solution"); int commitNum = 0; - string NuGetVersionCustom = "2.0.0.868"; + string NuGetVersionCustom = "2.0.0.869"; //if it's not a tagged release - append the commit number to the package version diff --git a/Src/RCommon.Core/EventHandling/Producers/InMemoryTransactionalEventRouter.cs b/Src/RCommon.Core/EventHandling/Producers/InMemoryTransactionalEventRouter.cs index 6e0075c1..a727a9ad 100644 --- a/Src/RCommon.Core/EventHandling/Producers/InMemoryTransactionalEventRouter.cs +++ b/Src/RCommon.Core/EventHandling/Producers/InMemoryTransactionalEventRouter.cs @@ -31,6 +31,7 @@ public async Task RouteEventsAsync(IEnumerable transactional try { Guard.IsNotNull(transactionalEvents, nameof(transactionalEvents)); + _logger.LogInformation("{0} is routing transactional events to event producers.", this.GetGenericTypeName()); // Seperate Async events from Sync Events var syncEvents = transactionalEvents.Where(x => x is ISyncEvent); diff --git a/Src/RCommon.Core/EventHandling/Producers/PublishWithEventBusEventProducer.cs b/Src/RCommon.Core/EventHandling/Producers/PublishWithEventBusEventProducer.cs index dfd49137..e5868e0f 100644 --- a/Src/RCommon.Core/EventHandling/Producers/PublishWithEventBusEventProducer.cs +++ b/Src/RCommon.Core/EventHandling/Producers/PublishWithEventBusEventProducer.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Extensions.Logging; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -10,14 +11,18 @@ namespace RCommon.EventHandling.Producers public class PublishWithEventBusEventProducer : IEventProducer { private readonly IEventBus _eventBus; + private readonly ILogger _logger; - public PublishWithEventBusEventProducer(IEventBus eventBus) + public PublishWithEventBusEventProducer(IEventBus eventBus, ILogger logger) { - _eventBus = eventBus; + _eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public async Task ProduceEventAsync(T @event, CancellationToken cancellationToken = default) where T : ISerializableEvent { + Guard.IsNotNull(@event, nameof(@event)); + _logger.LogInformation("{0} publishing event: {1}", new object[] { this.GetGenericTypeName(), @event }); await _eventBus.PublishAsync(@event); } } diff --git a/Src/RCommon.MassTransit/Producers/PublishWithMassTransitEventProducer.cs b/Src/RCommon.MassTransit/Producers/PublishWithMassTransitEventProducer.cs index bdd7c36a..9b1de1cb 100644 --- a/Src/RCommon.MassTransit/Producers/PublishWithMassTransitEventProducer.cs +++ b/Src/RCommon.MassTransit/Producers/PublishWithMassTransitEventProducer.cs @@ -1,4 +1,5 @@ using MassTransit; +using Microsoft.Extensions.Logging; using RCommon.EventHandling; using RCommon.EventHandling.Producers; using System; @@ -12,15 +13,18 @@ namespace RCommon.MassTransit.Producers public class PublishWithMassTransitEventProducer : IEventProducer { private readonly IBus _bus; + private readonly ILogger _logger; - public PublishWithMassTransitEventProducer(IBus bus) + public PublishWithMassTransitEventProducer(IBus bus, ILogger logger) { - _bus = bus; + _bus = bus ?? throw new ArgumentNullException(nameof(bus)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public async Task ProduceEventAsync(T @event, CancellationToken cancellationToken = default) where T : ISerializableEvent { - Console.WriteLine("{0} publishing event {1} to MassTransit", new object[] { this.GetGenericTypeName(), @event }); + Guard.IsNotNull(@event, nameof(@event)); + _logger.LogInformation("{0} publishing event: {1}", new object[] { this.GetGenericTypeName(), @event }); await _bus.Publish(@event, cancellationToken); } } diff --git a/Src/RCommon.MassTransit/Producers/SendWithMassTransitEventProducer.cs b/Src/RCommon.MassTransit/Producers/SendWithMassTransitEventProducer.cs index 56110e6e..087d725c 100644 --- a/Src/RCommon.MassTransit/Producers/SendWithMassTransitEventProducer.cs +++ b/Src/RCommon.MassTransit/Producers/SendWithMassTransitEventProducer.cs @@ -1,4 +1,6 @@ -using RCommon.EventHandling; +using MassTransit; +using Microsoft.Extensions.Logging; +using RCommon.EventHandling; using RCommon.EventHandling.Producers; using System; using System.Collections.Generic; @@ -10,15 +12,21 @@ namespace RCommon.MassTransit.Producers { public class SendWithMassTransitEventProducer : IEventProducer { - public SendWithMassTransitEventProducer() + + private readonly IBus _bus; + private readonly ILogger _logger; + + public SendWithMassTransitEventProducer(IBus bus, ILogger logger) { - + _bus = bus ?? throw new ArgumentNullException(nameof(bus)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } - public Task ProduceEventAsync(T @event, CancellationToken cancellationToken = default) - where T : ISerializableEvent + public async Task ProduceEventAsync(T @event, CancellationToken cancellationToken = default) where T : ISerializableEvent { - throw new NotImplementedException(); + Guard.IsNotNull(@event, nameof(@event)); + _logger.LogInformation("{0} sending event: {1}", new object[] { this.GetGenericTypeName(), @event }); + await _bus.Send(@event, cancellationToken); } } } diff --git a/Src/RCommon.Mediatr/Producers/PublishWithMediatREventProducer.cs b/Src/RCommon.Mediatr/Producers/PublishWithMediatREventProducer.cs index 843d3c01..83263234 100644 --- a/Src/RCommon.Mediatr/Producers/PublishWithMediatREventProducer.cs +++ b/Src/RCommon.Mediatr/Producers/PublishWithMediatREventProducer.cs @@ -1,4 +1,5 @@ using MediatR; +using Microsoft.Extensions.Logging; using RCommon.EventHandling; using RCommon.EventHandling.Producers; using RCommon.Mediator; @@ -14,16 +15,19 @@ namespace RCommon.MediatR.Producers public class PublishWithMediatREventProducer : IEventProducer { private readonly IMediatorService _mediatorService; + private readonly ILogger _logger; - public PublishWithMediatREventProducer(IMediatorService mediatorService) + public PublishWithMediatREventProducer(IMediatorService mediatorService, ILogger logger) { - _mediatorService = mediatorService; + _mediatorService = mediatorService ?? throw new ArgumentNullException(nameof(mediatorService)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public async Task ProduceEventAsync(TEvent @event, CancellationToken cancellationToken = default) where TEvent : ISerializableEvent { Guard.IsNotNull(@event, nameof(@event)); + _logger.LogInformation("{0} publishing event: {1}", new object[] { this.GetGenericTypeName(), @event }); await _mediatorService.Publish(@event, cancellationToken); } } diff --git a/Src/RCommon.Mediatr/Producers/SendWithMediatREventProducer.cs b/Src/RCommon.Mediatr/Producers/SendWithMediatREventProducer.cs index e0a55594..0b3a845c 100644 --- a/Src/RCommon.Mediatr/Producers/SendWithMediatREventProducer.cs +++ b/Src/RCommon.Mediatr/Producers/SendWithMediatREventProducer.cs @@ -8,22 +8,26 @@ using MediatR; using RCommon.MediatR.Subscribers; using RCommon.Mediator; +using Microsoft.Extensions.Logging; namespace RCommon.MediatR.Producers { public class SendWithMediatREventProducer : IEventProducer { private readonly IMediatorService _mediatorService; + private readonly ILogger _logger; - public SendWithMediatREventProducer(IMediatorService mediatorService) + public SendWithMediatREventProducer(IMediatorService mediatorService, ILogger logger) { - _mediatorService = mediatorService; + _mediatorService = mediatorService ?? throw new ArgumentNullException(nameof(mediatorService)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public async Task ProduceEventAsync(TEvent @event, CancellationToken cancellationToken = default) where TEvent : ISerializableEvent { Guard.IsNotNull(@event, nameof(@event)); + _logger.LogInformation("{0} sending event: {1}", new object[] { this.GetGenericTypeName(), @event }); await _mediatorService.Send(@event, cancellationToken); } } diff --git a/Src/RCommon.Wolverine/Producers/PublishWithWolverineEventProducer.cs b/Src/RCommon.Wolverine/Producers/PublishWithWolverineEventProducer.cs index 9d3b27f3..94b9d560 100644 --- a/Src/RCommon.Wolverine/Producers/PublishWithWolverineEventProducer.cs +++ b/Src/RCommon.Wolverine/Producers/PublishWithWolverineEventProducer.cs @@ -1,4 +1,6 @@ -using RCommon.EventHandling; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Configuration; +using RCommon.EventHandling; using RCommon.EventHandling.Producers; using System; using System.Collections.Generic; @@ -12,14 +14,18 @@ namespace RCommon.Wolverine.Producers public class PublishWithWolverineEventProducer : IEventProducer { private readonly IMessageBus _messageBus; + private readonly ILogger _logger; - public PublishWithWolverineEventProducer(IMessageBus messageBus) + public PublishWithWolverineEventProducer(IMessageBus messageBus, ILogger logger) { - _messageBus = messageBus; + _messageBus = messageBus ?? throw new ArgumentNullException(nameof(messageBus)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public async Task ProduceEventAsync(T @event, CancellationToken cancellationToken = default) where T : ISerializableEvent { + Guard.IsNotNull(@event, nameof(@event)); + _logger.LogInformation("{0} publishing event: {1}", new object[] { this.GetGenericTypeName(), @event }); await _messageBus.PublishAsync(@event); } } diff --git a/Src/RCommon.Wolverine/Producers/SendWithWolverineEventProducer.cs b/Src/RCommon.Wolverine/Producers/SendWithWolverineEventProducer.cs index 5c876ba5..d0fa3b73 100644 --- a/Src/RCommon.Wolverine/Producers/SendWithWolverineEventProducer.cs +++ b/Src/RCommon.Wolverine/Producers/SendWithWolverineEventProducer.cs @@ -1,4 +1,5 @@ -using RCommon.EventHandling; +using Microsoft.Extensions.Logging; +using RCommon.EventHandling; using RCommon.EventHandling.Producers; using System; using System.Collections.Generic; @@ -12,15 +13,19 @@ namespace RCommon.Wolverine.Producers public class SendWithWolverineEventProducer : IEventProducer { private readonly IMessageBus _messageBus; + private readonly ILogger _logger; - public SendWithWolverineEventProducer(IMessageBus messageBus) + public SendWithWolverineEventProducer(IMessageBus messageBus, ILogger logger) { - _messageBus = messageBus; + _messageBus = messageBus ?? throw new ArgumentNullException(nameof(messageBus)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public async Task ProduceEventAsync(T @event, CancellationToken cancellationToken = default) where T : ISerializableEvent { + Guard.IsNotNull(@event, nameof(@event)); + _logger.LogInformation("{0} sending event: {1}", new object[] { this.GetGenericTypeName(), @event }); await _messageBus.SendAsync(@event); } }