Adds Decorator, wip

This commit is contained in:
Janus C. H. Knudsen 2025-03-11 00:28:06 +01:00
parent a86a2d7ade
commit 49f9b99ee1
9 changed files with 112 additions and 45 deletions

View file

@ -1,25 +1,41 @@
using Microsoft.ApplicationInsights;
using System.Diagnostics;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
namespace PlanTempus.Components.Users.Create;
public class CreateUserHandlerDecorator : ICommandHandler<CreateUserCommand, CreateUserResult>
public class CreateUserHandlerDecorator(
ICommandHandler<CreateUserCommand, CreateUserResult> decoratedHandler,
TelemetryClient telemetryClient)
: ICommandHandler<CreateUserCommand, CreateUserResult>, ICommandHandlerDecorator
{
private readonly ICommandHandler<CreateUserCommand, CreateUserResult> _decoratedHandler;
private readonly TelemetryClient _telemetryClient;
public CreateUserHandlerDecorator(
ICommandHandler<CreateUserCommand, CreateUserResult> decoratedHandler,
TelemetryClient telemetryClient)
{
_decoratedHandler = decoratedHandler;
_telemetryClient = telemetryClient;
}
public async Task<CreateUserResult> Handle(CreateUserCommand command)
{
_telemetryClient.TrackTrace($"Before handling {nameof(CreateUserCommand)}");
var result = await _decoratedHandler.Handle(command);
_telemetryClient.TrackTrace($"After handling {nameof(CreateUserCommand)}");
return result;
// 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;
}
}
}
}
}