Adds Generic CommandHandlerDecorator

This commit is contained in:
Janus C. H. Knudsen 2025-03-12 00:13:53 +01:00
parent 49f9b99ee1
commit 64e696dc5a
21 changed files with 131 additions and 110 deletions

View file

@ -1,41 +0,0 @@
using System.Diagnostics;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
namespace PlanTempus.Components.Users.Create;
public class CreateUserHandlerDecorator(
ICommandHandler<CreateUserCommand, CreateUserResult> decoratedHandler,
TelemetryClient telemetryClient)
: ICommandHandler<CreateUserCommand, CreateUserResult>, ICommandHandlerDecorator
{
public async Task<CreateUserResult> Handle(CreateUserCommand command)
{
// var correlationId = Activity.Current?.RootId ?? command.CorrelationId;
using (var operation =
telemetryClient.StartOperation<RequestTelemetry>($"Handle {nameof(CreateUserCommand)}",
command.CorrelationId.ToString()))
{
try
{
operation.Telemetry.Properties["CorrelationId"] = command.CorrelationId.ToString();
var result = await decoratedHandler.Handle(command);
operation.Telemetry.Success = true;
return result;
}
catch (Exception ex)
{
operation.Telemetry.Success = false;
telemetryClient.TrackException(ex, new Dictionary<string, string>
{
["CommandType"] = nameof(CreateUserCommand)
});
throw;
}
}
}
}