-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMyExecutionEventListener.cs
More file actions
101 lines (88 loc) · 3.92 KB
/
MyExecutionEventListener.cs
File metadata and controls
101 lines (88 loc) · 3.92 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using HotChocolate.Execution;
using HotChocolate.Execution.Instrumentation;
using System.Text;
namespace DemoChoco
{
public class MyExecutionEventListener : ExecutionDiagnosticEventListener
{
private readonly ILogger<MyExecutionEventListener> _logger;
public MyExecutionEventListener(ILogger<MyExecutionEventListener> logger)
=> _logger = logger;
public override IDisposable ExecuteRequest(IRequestContext context)
{
var start = DateTime.UtcNow;
return new RequestScope(start, _logger, context);
}
public override void RequestError(IRequestContext context,
Exception exception)
{
_logger.LogError(exception, "A request error occured!");
}
}
public class RequestScope : IDisposable
{
private readonly ILogger _logger;
private readonly DateTime _start;
private readonly IRequestContext _context;
public RequestScope(DateTime start, ILogger logger,
IRequestContext context)
{
_start = start;
_logger = logger;
_context = context;
}
// this is invoked at the end of the `ExecuteRequest` operation
public void Dispose()
{
var end = DateTime.UtcNow;
var elapsed = end - _start;
_logger.LogInformation("Request finished after {Ticks} ticks",
elapsed.Ticks);
// when the request is finished it will dispose the activity scope and
// this is when we print the parsed query.
if (_context.Document is not null)
{
// we just need to do a ToString on the Document which represents the parsed
// GraphQL request document.
StringBuilder stringBuilder = new(_context.Document.ToString(true));
stringBuilder.AppendLine();
if (_context.Variables != null)
{
var variablesConcrete = _context.Variables!.ToList();
if (variablesConcrete.Count > 0)
{
stringBuilder.AppendFormat($"Variables {Environment.NewLine}");
try
{
foreach (var variableValue in _context.Variables!)
{
string PadRightHelper(string existingString, int lengthToPadTo)
{
if (string.IsNullOrEmpty(existingString))
{
return "".PadRight(lengthToPadTo);
}
if (existingString.Length > lengthToPadTo)
{
return existingString.Substring(0, lengthToPadTo);
}
return existingString + " ".PadRight(lengthToPadTo - existingString.Length);
}
stringBuilder.AppendFormat(
$" {PadRightHelper(variableValue.Name, 20)} : {PadRightHelper(variableValue.Value.ToString(), 20)}: {variableValue.Type}");
stringBuilder.AppendFormat($"{Environment.NewLine}");
}
}
catch
{
// all input type records will land here.
stringBuilder.Append(" Formatting Variables Error. Continuing...");
stringBuilder.AppendFormat($"{Environment.NewLine}");
}
}
}
_logger.LogInformation(stringBuilder.ToString());
}
}
}
}