使用 serilog,我尝试通过 appSettings.json 添加我自己的自定义丰富器,但没有成功。
我完全按照书本,遵循文档。我还遵循了这样的非官方介绍https://www.ctrlaltdan.com/2018/08/14/custom-serilog-enrichers/
上面的介绍适用于金块丰富器(如 Serilog.Enrichers.Environment、Serilog.Enrichers.Thread 等),但不适用于我的自定义丰富器。这是相关代码:
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>();
}
} …
Run Code Online (Sandbox Code Playgroud)