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

46 lines
1.7 KiB
C#
Raw Normal View History

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
{
2025-03-11 00:28:06 +01:00
// var correlationId = Activity.Current?.RootId ?? command.CorrelationId;
using (var operation =
2025-03-12 00:13:53 +01:00
telemetryClient.StartOperation<RequestTelemetry>($"Handle {decoratedHandler.GetType().FullName}",
2025-03-11 00:28:06 +01:00
command.CorrelationId.ToString()))
{
try
{
operation.Telemetry.Properties["CorrelationId"] = command.CorrelationId.ToString();
2025-03-12 00:13:53 +01:00
operation.Telemetry.Properties["TransactionId"] = command.TransactionId.ToString();
2025-03-11 00:28:06 +01:00
var result = await decoratedHandler.Handle(command);
2025-03-12 00:13:53 +01:00
operation.Telemetry.Properties["RequestId"] = result.RequestId.ToString();
2025-03-11 00:28:06 +01:00
operation.Telemetry.Success = true;
return result;
}
catch (Exception ex)
{
operation.Telemetry.Success = false;
telemetryClient.TrackException(ex, new Dictionary<string, string>
{
2025-03-12 00:13:53 +01:00
["CorrelationId"] = command.CorrelationId.ToString(),
["Command"] = command.GetType().Name,
["CommandHandler"] = decoratedHandler.GetType().FullName
2025-03-11 00:28:06 +01:00
});
throw;
}
}
2025-03-10 15:56:22 +01:00
}
2025-03-11 00:28:06 +01:00
}