-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDependencyTelemetryRequestHandlerDecorator.cs
More file actions
41 lines (36 loc) · 1.55 KB
/
DependencyTelemetryRequestHandlerDecorator.cs
File metadata and controls
41 lines (36 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
namespace softaware.Cqs.Decorators.ApplicationInsights;
/// <summary>
/// Creates an application insights dependency telementry for every request handler.
/// This creates a CQS "call stack" in Application Insights end-to-end transaction details.
/// </summary>
/// <remarks>
/// Requires the <see cref="TelemetryClient"/> to be registered in the dependency injection container.
/// </remarks>
public class DependencyTelemetryRequestHandlerDecorator<TRequest, TResult> : IRequestHandler<TRequest, TResult>
where TRequest : IRequest<TResult>
{
private readonly TelemetryClient telemetryClient;
private readonly IRequestHandler<TRequest, TResult> decoratee;
public DependencyTelemetryRequestHandlerDecorator(
TelemetryClient telemetryClient,
IRequestHandler<TRequest, TResult> decoratee)
{
this.telemetryClient = telemetryClient ?? throw new ArgumentNullException(nameof(telemetryClient));
this.decoratee = decoratee ?? throw new ArgumentNullException(nameof(decoratee));
}
public Task<TResult> HandleAsync(TRequest request, CancellationToken cancellationToken)
{
using var operation = this.telemetryClient.StartOperation<DependencyTelemetry>(request.GetType().Name);
operation.Telemetry.Type = "CQS";
try
{
return this.decoratee.HandleAsync(request, cancellationToken);
}
finally
{
this.telemetryClient.StopOperation(operation);
}
}
}