forked from soroushkavousi/fireplace-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResolverLoggingFieldMiddleware.cs
53 lines (48 loc) · 1.74 KB
/
ResolverLoggingFieldMiddleware.cs
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
42
43
44
45
46
47
48
49
50
51
52
53
using FireplaceApi.Application.Extensions;
using FireplaceApi.Domain.Extensions;
using HotChocolate.Resolvers;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
namespace FireplaceApi.Application.Middlewares;
public class ResolverLoggingFieldMiddleware
{
private readonly FieldDelegate _next;
private readonly ILogger<ResolverLoggingFieldMiddleware> _logger;
public ResolverLoggingFieldMiddleware(FieldDelegate next,
ILogger<ResolverLoggingFieldMiddleware> logger)
{
_next = next;
_logger = logger;
}
public async Task InvokeAsync(IMiddlewareContext context)
{
if (!context.IsApiResolver())
{
await _next(context);
return;
}
var sw = Stopwatch.StartNew();
var inputs = context.GetResolverInputs();
var path = context.Path.ToString();
_logger.LogAppInformation(message: path, title: "RESOLVER_INPUT", parameters: inputs);
await _next(context);
_logger.LogAppInformation(sw: sw, message: path, title: "RESOLVER_OUTPUT", parameters: context.Result);
}
}
public static class ResolverLoggingFieldMiddlewareExtensions
{
internal static Dictionary<string, object> GetResolverInputs(this IMiddlewareContext context)
{
var arguments = (IReadOnlyDictionary<string, ArgumentValue>)context.GetType().GetProperty("Arguments").GetValue(context, null);
var argumentKeys = arguments.Keys;
var inputs = new Dictionary<string, object>();
foreach (var key in argumentKeys)
{
var value = context.ArgumentValue<object>(key);
inputs.Add(key, value);
}
return inputs;
}
}