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

145
Views
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
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!