Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

595
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda