PlanTempusApp/PlanTempus.Components/Accounts/Create/CommandHandlerDecorator.cs

44 lines
1.6 KiB
C#
Raw Permalink Normal View History

using System.Diagnostics;
2025-03-11 00:28:06 +01:00
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.Accounts.Create;
2025-03-10 15:56:22 +01:00
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
{
command.TransactionId = Guid.NewGuid();
using var operation =
2025-03-12 00:13:53 +01:00
telemetryClient.StartOperation<RequestTelemetry>($"Handle {decoratedHandler.GetType().FullName}",
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
}
}