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

154
Visualizações
Multi-threaded logging in log4net & C#

We have a multi-threaded task execution service which is frequently executing tasks in parallel. As part of our log messages we want to have the task name as well as an execution id (ContextKey). Currently trying to set context key in the task constructor:

public AsyncTaskBase(ILogger logger, IContextKeyLocator locator)
{
    _Logger = logger;
    _ContextKeyLocator = locator;
    contextKey = _ContextKeyLocator.GenerateAndSetContextKey();
    log4net.ThreadContext.Properties["ContextKey"] = contextKey;
}

From there we leave it up to the log4net appender pattern to log the context key

<conversionPattern value="%date [%thread] %-5level [%property{ContextKey}] - %message" />

And what we see in the logs are series of log messages with the same thread/contextKey values that were most definitely not from the same task.

So my question is how do I handle this kind of situation? We have properties we'd like to have in the log message that are contextual only to the current execution, but it also needs to be thread safe. Any help, guidance, direction, would be great. Thank you in advance!

over 4 years ago · Santiago Trujillo
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