using System.Diagnostics; using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights.DataContracts; using PlanTempus.Core.CommandQueries; namespace PlanTempus.Components.Users.Create; public class CommandHandlerDecorator( ICommandHandler decoratedHandler, TelemetryClient telemetryClient) : ICommandHandler, ICommandHandlerDecorator where TCommand : ICommand { public async Task Handle(TCommand command) { // var correlationId = Activity.Current?.RootId ?? command.CorrelationId; using (var operation = telemetryClient.StartOperation($"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 { ["CorrelationId"] = command.CorrelationId.ToString(), ["Command"] = command.GetType().Name, ["CommandHandler"] = decoratedHandler.GetType().FullName }); throw; } } } }