Adds Decorator, wip
This commit is contained in:
parent
a86a2d7ade
commit
49f9b99ee1
9 changed files with 112 additions and 45 deletions
|
|
@ -8,16 +8,6 @@ using PlanTempus.Core.Telemetry;
|
|||
|
||||
namespace PlanTempus.Components.Users.Create
|
||||
{
|
||||
public interface ICommandHandler
|
||||
{
|
||||
Task<TCommandResult> Handle<TCommand, TCommandResult>(TCommand command );
|
||||
}
|
||||
|
||||
public interface ICommandHandler<in T, TResult>
|
||||
{
|
||||
Task<TResult> Handle(T input);
|
||||
}
|
||||
|
||||
public class CreateUserHandler(
|
||||
TelemetryClient telemetryClient,
|
||||
IDatabaseOperations databaseOperations,
|
||||
|
|
@ -50,7 +40,6 @@ namespace PlanTempus.Components.Users.Create
|
|||
|
||||
var result = data.First();
|
||||
|
||||
telemetryClient.TrackTrace(GetType().Name, result.Format());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue