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?
