2025-03-11 00:28:06 +01:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using Microsoft.ApplicationInsights;
|
|
|
|
|
|
using Microsoft.ApplicationInsights.DataContracts;
|
2025-03-12 00:13:53 +01:00
|
|
|
|
using PlanTempus.Core.CommandQueries;
|
2025-03-10 15:56:22 +01:00
|
|
|
|
|
|
|
|
|
|
namespace PlanTempus.Components.Users.Create;
|
|
|
|
|
|
|
2025-03-12 00:13:53 +01:00
|
|
|
|
public class CommandHandlerDecorator<TCommand>(
|
|
|
|
|
|
ICommandHandler<TCommand> decoratedHandler,
|
|
|
|
|
|
TelemetryClient telemetryClient) : ICommandHandler<TCommand>, ICommandHandlerDecorator where TCommand : ICommand
|
2025-03-10 15:56:22 +01:00
|
|
|
|
{
|
2025-03-12 00:13:53 +01:00
|
|
|
|
public async Task<CommandResponse> Handle(TCommand command)
|
2025-03-10 15:56:22 +01:00
|
|
|
|
{
|
2026-01-08 21:51:43 +01:00
|
|
|
|
command.TransactionId = Guid.NewGuid();
|
|
|
|
|
|
using var operation =
|
2025-03-12 00:13:53 +01:00
|
|
|
|
telemetryClient.StartOperation<RequestTelemetry>($"Handle {decoratedHandler.GetType().FullName}",
|
2026-01-08 21:51:43 +01:00
|
|
|
|
command.CorrelationId.ToString());
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
operation.Telemetry.Properties["CorrelationId"] = command.CorrelationId.ToString();
|
|
|
|
|
|
operation.Telemetry.Properties["TransactionId"] = command.TransactionId.ToString();
|
|
|
|
|
|
|
|
|
|
|
|
var result = await decoratedHandler.Handle(command);
|
|
|
|
|
|
|
|
|
|
|
|
operation.Telemetry.Properties["RequestId"] = result.RequestId.ToString();
|
|
|
|
|
|
operation.Telemetry.Success = true;
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
operation.Telemetry.Success = false;
|
|
|
|
|
|
|
|
|
|
|
|
telemetryClient.TrackException(ex, new Dictionary<string, string>
|
|
|
|
|
|
{
|
|
|
|
|
|
["CorrelationId"] = command.CorrelationId.ToString(),
|
|
|
|
|
|
["Command"] = command.GetType().Name,
|
|
|
|
|
|
["CommandHandler"] = decoratedHandler.GetType().FullName
|
|
|
|
|
|
});
|
|
|
|
|
|
throw;
|
2025-03-11 00:28:06 +01:00
|
|
|
|
}
|
2025-03-10 15:56:22 +01:00
|
|
|
|
}
|
2025-03-11 00:28:06 +01:00
|
|
|
|
}
|