Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

592
Views
Serilog in .Net: Adding custom enricher via appSettings.json doesn't work

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:

enter image description here

I'll note that adding my enrihcer in code working fine. Any suggestion?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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" ]
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!