Add PostprocessingBehavior for action result logging
Introduced PostprocessingBehavior as an IPipelineBehavior for InvokeRecActionViewCommand. This behavior handles post-processing by executing optional queries, serializing results, and logging outcomes via InsertResultCommand. It also manages error handling based on the action's ErrorAction setting, ensuring consistent result tracking after command execution.
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
using MediatR;
|
||||
using ReC.Application.Common.Interfaces;
|
||||
using ReC.Application.RecActions.Commands;
|
||||
using ReC.Application.Results.Commands;
|
||||
using ReC.Domain.Constants;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ReC.Application.Common.Behaviors.Action;
|
||||
|
||||
public class PostprocessingBehavior(IRecDbContext context, ISender sender) : IPipelineBehavior<InvokeRecActionViewCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(InvokeRecActionViewCommand request, RequestHandlerDelegate<bool> next, CancellationToken cancel)
|
||||
{
|
||||
var response = false;
|
||||
try
|
||||
{
|
||||
response = await next(cancel);
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (request.Action.ErrorAction == ErrorAction.Stop)
|
||||
return false;
|
||||
}
|
||||
|
||||
string? info = null, error = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (request.Action.PostprocessingQuery is string query)
|
||||
{
|
||||
var result = await context.ExecuteDynamicSqlAsync(query, cancel);
|
||||
info = JsonSerializer.Serialize(result);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
error = ex.ToString();
|
||||
}
|
||||
|
||||
await sender.Send(new InsertResultCommand()
|
||||
{
|
||||
ActionId = request.Action.Id,
|
||||
Info = info,
|
||||
Error = error
|
||||
}, cancel);
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user