[英]How to write the right appsettings.json file for custom Enricher within argument
I have a custom Enricher: CorrelationIdsEnricher
in order to write the CorrelationId
and RequestId
to log, and its constructor has an argument: ICorrelationContextProvider
for passing the correlation context provider. 我有一个自定义的Enricher: CorrelationIdsEnricher
,以便将CorrelationId
和RequestId
写入日志,其构造函数具有一个参数: ICorrelationContextProvider
用于传递相关上下文提供程序。
In my project, i config the serilog by reading the appsettings.json
config file. 在我的项目中,我通过阅读appsettings.json
配置文件appsettings.json
配置appsettings.json
。 And here's the config file: 这是配置文件:
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Common.Logging", "Common.Correlation" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] [{RequestId} {CorrelationId}] {Message}{NewLine}{Exception}",
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console"
}
}
],
"Enrich": [
"FromLogContext",
{
"Name": "WithCorrelationIds",
"Args": {
"provider": "Correlation.ServerContextProvider::Default, Common.Correlation"
}
}
],
}
}
However it can not set the CorrelationIdsEnricher
correctly. 但是,它不能正确设置CorrelationIdsEnricher
。
Does anyone know why? 有人知道为什么吗?
The reason is that i forgot to add the WithCorrelationIds
extension method. 原因是我忘记添加WithCorrelationIds
扩展方法。 I thought implementing the CorrelationIdsEnricher
is enough at first. 我认为首先实现CorrelationIdsEnricher
就足够了。
After looking into the source code ConfigurationReader.cs of serilog-settings-configuration
, i found that i forget to implement extensions WithCorrelationIds
. 在查看serilog-settings-configuration
的源代码ConfigurationReader.cs之后,我发现我忘记实现扩展WithCorrelationIds
。
That's to say, in order to support initializing serilog from configuration for our custom Enricher
and Sink
, we need to not only create the Enricher
and Sink
class but also implement their LoggerSinkConfiguration
extensions. 这就是说,为了支持从配置初始化serilog为我们定制Enricher
和Sink
,我们需要不仅创造Enricher
和Sink
类,但也实现了自己的LoggerSinkConfiguration
扩展。
Attached the CorrelationIdsEnricher
implementation: 附加了CorrelationIdsEnricher
实现:
using Common.Correlation;
using Serilog.Core;
using Serilog.Events;
namespace Common.Logging.Enrichers
{
public class CorrelationIdsEnricher : ILogEventEnricher
{
private readonly ICorrelationContextProvider _provider;
public CorrelationIdsEnricher(ICorrelationContextProvider provider)
{
_provider = provider;
}
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
var (requestId, correlationId) = GetIds();
logEvent.AddPropertyIfAbsent(
propertyFactory.CreateProperty("RequestId", requestId, false));
logEvent.AddPropertyIfAbsent(
propertyFactory.CreateProperty("CorrelationId", correlationId, false));
}
private (string requestId, string correlationId) GetIds()
{
var ctx = _provider.Context;
return (ctx?.RequestId ?? string.Empty, ctx?.CorrelationId ?? string.Empty);
}
}
}
And LoggerEnrichmentConfiguration
Extensions: 和LoggerEnrichmentConfiguration
扩展:
public static LoggerConfiguration WithCorrelationIds(
this LoggerEnrichmentConfiguration enrichmentConfiguration,
ICorrelationContextProvider provider)
{
return enrichmentConfiguration.With(new CorrelationIdsEnricher(provider));
}
And here's a mocked correlation provider: 这是一个模拟的关联提供程序:
public class CorrelationContextProvider : ICorrelationContextProvider
{
public static ICorrelationContextProvider Default { get; } = new CorrelationContextProvider();
public ICorrelationContext Context => new CorrelationContext();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.