Reputation: 135
Using serilog, I'm trying to add custom enricher of my own via appSettings.json without success.
I went exactly by the book, following the documentation. I also followed non official introduction like this https://www.ctrlaltdan.com/2018/08/14/custom-serilog-enrichers/
The introduction above work fine with nuggets enrichers (like Serilog.Enrichers.Environment, Serilog.Enrichers.Thread and so on) but doesn't work with my custom enricher. Here is the relevant code:
public class ClassNameEnricher: ILogEventEnricher
{
private string GetClassName()
{
StackTrace trace = new StackTrace();
bool foundFrame = false;
int currentFrame = 0;
while (!foundFrame)
{
if (trace.GetFrame(currentFrame).GetMethod().ReflectedType.FullName == typeof(Logger).FullName)
{
foundFrame = true;
}
currentFrame++;
}
return trace.GetFrame(currentFrame).GetMethod().ReflectedType.FullName;
}
public virtual void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
LogEventProperty className = propertyFactory.CreateProperty("ClassName", GetClassName());
logEvent.AddPropertyIfAbsent(className);
}
}
public static class LoggerEnrichmentConfigurationExtensions
{
public static LoggerConfiguration WithClassName(this LoggerEnrichmentConfiguration enrich)
{
return enrich.With<ClassNameEnricher>();
}
}
and the settings:
I'll note that adding my enrihcer in code working fine. Any suggestion?
Upvotes: 5
Views: 7650
Reputation: 23254
You have to include the name of your assembly in the "Using"
section.
From the Serilog
documentation
Using section contains list of assemblies in which configuration methods (WriteTo.File(), Enrich.WithThreadId()) reside.
Notice in that article you are referring to, that assembly configuration is present:
"Using": [ "Example.Assembly.Name" ]
Upvotes: 11