Skip to content

Commit

Permalink
Updated mediatr logging, and unitofwork behaviors to accomodate for r…
Browse files Browse the repository at this point in the history
…equests with and without responses
  • Loading branch information
jasonmwebb-lv committed Apr 23, 2024
1 parent 9b1b19a commit aaabf5c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 20 additions & 3 deletions Src/RCommon.Mediatr/Behaviors/LoggingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,28 @@

namespace RCommon.Mediator.MediatR.Behaviors
{
public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
public class LoggingRequestBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest
{
private readonly ILogger<LoggingRequestBehavior<TRequest, TResponse>> _logger;
public LoggingRequestBehavior(ILogger<LoggingRequestBehavior<TRequest, TResponse>> logger) => _logger = logger;


public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> 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<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly ILogger<LoggingBehavior<TRequest, TResponse>> _logger;
public LoggingBehavior(ILogger<LoggingBehavior<TRequest, TResponse>> logger) => _logger = logger;
private readonly ILogger<LoggingRequestWithResponseBehavior<TRequest, TResponse>> _logger;
public LoggingRequestWithResponseBehavior(ILogger<LoggingRequestWithResponseBehavior<TRequest, TResponse>> logger) => _logger = logger;


public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
Expand Down
57 changes: 52 additions & 5 deletions Src/RCommon.Mediatr/Behaviors/UnitOfWorkBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

namespace RCommon.Mediator.MediatR.Behaviors
{
public class UnitOfWorkBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
public class UnitOfWorkRequestBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest
{
private readonly ILogger<UnitOfWorkBehavior<TRequest, TResponse>> _logger;
private readonly ILogger<UnitOfWorkRequestBehavior<TRequest, TResponse>> _logger;
private readonly IUnitOfWorkFactory _unitOfWorkScopeFactory;
private readonly IUnitOfWorkManager _unitOfWorkManager;

public UnitOfWorkBehavior(IUnitOfWorkFactory unitOfWorkScopeFactory, IUnitOfWorkManager unitOfWorkManager,
ILogger<UnitOfWorkBehavior<TRequest, TResponse>> logger)
public UnitOfWorkRequestBehavior(IUnitOfWorkFactory unitOfWorkScopeFactory, IUnitOfWorkManager unitOfWorkManager,
ILogger<UnitOfWorkRequestBehavior<TRequest, TResponse>> logger)
{
_unitOfWorkScopeFactory = unitOfWorkScopeFactory ?? throw new ArgumentException(nameof(IUnitOfWorkFactory));
_unitOfWorkManager = unitOfWorkManager ?? throw new ArgumentException(nameof(IUnitOfWorkManager));
Expand Down Expand Up @@ -44,6 +44,53 @@ public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TRe
}


return response;
}
catch (Exception ex)
{
_logger.LogError(ex, "ERROR Handling transaction for {CommandName} ({@Command})", typeName, request);

throw;
}
}
}

public class UnitOfWorkRequestWithResponseBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly ILogger<UnitOfWorkRequestWithResponseBehavior<TRequest, TResponse>> _logger;
private readonly IUnitOfWorkFactory _unitOfWorkScopeFactory;
private readonly IUnitOfWorkManager _unitOfWorkManager;

public UnitOfWorkRequestWithResponseBehavior(IUnitOfWorkFactory unitOfWorkScopeFactory, IUnitOfWorkManager unitOfWorkManager,
ILogger<UnitOfWorkRequestWithResponseBehavior<TRequest, TResponse>> 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<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> 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)
Expand Down
6 changes: 4 additions & 2 deletions Src/RCommon.Mediatr/MediatRBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public static void AddRequest<TRequest, TResponse, TEventHandler>(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)
Expand All @@ -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<,>));
}

}
Expand Down

0 comments on commit aaabf5c

Please sign in to comment.