graphql - How can I log resolver/operation name and duration in HotChocolate?
问题描述
I'm trying to log some basic gql method details - resolver/operation name and duration. I've started looking at using .AddHttpRequestInterceptor((context, executor, builder, ct)
and getting the info from the builder, but even though I can see it in the debugger, the method name is buried in private members like:
((HotChocolate.Execution.QueryRequestBuilder)builder)._query.Document.Definitions[0].SelectionSet.Selections[0].Name.Value
I'm sure there's an easier and better way to hook into the pipeline to get the method name and log it with the call duration.
I found an article written about GraphQL.Net that uses DefaultGraphQLExecuter -
public class GraphQLExecutorWithDiagnostics<TSchema> : DefaultGraphQLExecuter<TSchema>
which provides an operationName parameter within the
Task<ExecutionResult> ExecuteAsync(
, which looks ideal.
I'll be logging to AppInsights, but that's not relevant for now, I just want to get the info first. I'm using v11.0.8
解决方案
What you are looking for is the DiagnosticEventListener
You can just extend this base class and override the methods that you need for you logging.
public class CustomDiagnosticListener : DiagnosticEventListener
{
public override IActivityScope ExecuteRequest(IRequestContext context)
{
return EmptyScope;
}
public virtual IActivityScope ResolveFieldValue(IMiddlewareContext context)
{
return EmptyScope;
}
}
To use this diagnostic listener you have to add it to the schema
services.AddGraphQLServer()
...
.AddDiagnosticEventListener<CustomDiagnosticListener>()
In case you have dependencies that you listener has to reslove you have to reslove them manually:
services.AddGraphQLServer()
...
.AddDiagnosticEventListener<CustomDiagnosticListener>(
sp => new CustomDiagnosticListener(
sp.GetApplicationService<MyDependency>()))
推荐阅读
- node.js - 如何使用 async/await 在 node.js 中异步创建ReadStream
- c# - LINQ 表达式中的 C# 闭包
- python-3.x - 在 Python 中使用生成器进行广度优先树遍历
- python - Pandas xlrd 引擎通过了仍然值错误
- file - 无法使用 int 21h 3Dh 打开文件
- ios - 是什么导致了这种类型的错误。xxxxxx 没有可见的@interface。根本原因。不只是这一个
- javascript - 你如何强制内容脚本等待所有元素(iFrames)完成加载?
- r - How to assign a same value as the argument to a vector of functions in R?
- javascript - 禁用函数中的未知变量
- windows-installer - Chromium - mini_installer 忽略品牌