[英]How to get Serilog to use custom enricher from json config file
I wish to use formatted UTC timestamps in my Serilog output.我希望在我的 Serilog output 中使用格式化的 UTC 时间戳。 I have written a custom enricher that works fine when called from C# code.
我编写了一个自定义浓缩器,当从 C# 代码调用时,它可以正常工作。
public class UtcTimestampEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory pf)
{
logEvent.AddPropertyIfAbsent(pf.CreateProperty("UtcTimestamp", logEvent.Timestamp.UtcDateTime));
}
}
....
var loggerConfig = new LoggerConfiguration().MinimumLevel.Debug()
.Enrich.FromLogContext()
.Enrich.With(new UtcTimestampEnricher())
.Filter
.ByIncludingOnly( expr) // need this .Filter to ensure that
// Serilog.Filters.Expressions.dll gets loaded, else filters in config file get ignored
.WriteTo.Console(
outputTemplate: "[{UtcTimestamp:HH:mm:ss.fff} {Level:u3} {Subsystem}] {Message:lj}{NewLine}{Exception}",
restrictedToMinimumLevel: LogEventLevel.Information);
Log.Logger = loggerConfig.CreateLogger();
Now I wish to use the utcTimestamp enricher while configuring the logger from my custom json config file.现在我希望在从我的自定义 json 配置文件配置记录器时使用 utcTimestamp 丰富器。
var jsonLogconfiguration = new ConfigurationBuilder()
.AddJsonFile(logconfigFname)
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(jsonLogconfiguration)
.CreateLogger();
My json config file我的 json 配置文件
{
"Serilog": {
"Using": [
"Serilog.Sinks.Console"
],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "{UtcTimestamp:yyyy,MM,dd,HH,mm,ss,fff },{Level:u3},{Subsystem},{Message:lj}{NewLine}{Exception}"
}
}
],
"Enrich": [ "FromLogContext" , "UtcTimestampEnricher"],
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "Subsystem = 'Config' or Subsystem = 'Admin' "
}
}
]
}
}
The message I get on the console: ( I have previously called serilog selflog to get serilog debug messages about itself)我在控制台上收到的消息:(我之前调用了 serilog selflog 来获取关于自身的 serilog 调试消息)
Serilog.Debugging.SelfLog.Enable(msg => Console.WriteLine(msg));
Serilog debug output. Serilog 调试 output。
2020-06-05T09:34:01.3898891Z Unable to find a method called UtcTimestampEnricher. Candidate methods are:
Serilog.LoggerConfiguration When(Serilog.Configuration.LoggerEnrichmentConfiguration, System.String, System.Action`1[Serilog.Configuration.LoggerEnrichmentConfiguration])
Serilog.LoggerConfiguration With(Serilog.Configuration.LoggerEnrichmentConfiguration, Serilog.Core.ILogEventEnricher)
Serilog.LoggerConfiguration FromLogContext(Serilog.Configuration.LoggerEnrichmentConfiguration)
I got similar output when I tried to use当我尝试使用时,我得到了类似的 output
"Enrich": [ "FromLogContext" , "UtcTimestamp"],
Even though the problem has been already solved I want to share my 2 cents regarding this problem.即使问题已经解决,我也想分享我关于这个问题的 2 美分。
Whenever you create a custom Enricher
you have two options how you can register that:每当您创建自定义
Enricher
时,您有两个选择如何注册它:
public class UtcTimestampEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory pf)
{
...
}
}
Log.Logger = new LoggerConfiguration()
.Enrich.With<UtcTimestampEnricher>()
.CreateLogger()
public class UtcTimestampEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory pf)
{
...
}
}
public static class LoggingExtensions
{
public static LoggerConfiguration WithUtcTimestamp(
this LoggerEnrichmentConfiguration enrich)
{
if (enrich == null)
throw new ArgumentNullException(nameof(enrich));
return enrich.With<UtcTimestampEnricher>();
}
}
{
"Serilog": {
...,
"Using": [ "Your.Assembly.Name" ],
"Enrich": [ "FromLogContext", "WithUtcTimestamp" ]
},
So, as you can see what you need to register is the registration helper method (not the enricher itself) in case of configuration based setup.因此,正如您所看到的,在基于配置的设置的情况下,您需要注册的是注册帮助器方法(而不是丰富器本身)。
I finally figured this out, and could get the custom enricher to work by specifying it in C# code我终于想通了,并且可以通过在 C# 代码中指定自定义浓缩器来工作
var jsonLogconfiguration = new ConfigurationBuilder()
.AddJsonFile(logconfigFname)
.Build();
Log.Logger = new LoggerConfiguration()
.Enrich.With(new UtcTimestampEnricher()) // is necessary
.ReadFrom.Configuration(jsonLogconfiguration)
.CreateLogger()
and removing the enrich with from the config file并从配置文件中删除丰富
"Enrich": [ "FromLogContext" ],
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.