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!