Why ThreadLocal<T> fails when used in asynchronous C# code:
When execution resumes after an await, the continuation frequently runs on a different thread from the .NET ThreadPool. ThreadLocal<T> stores data on the physical OS thread, losing your context!
AsyncLocal<T> Context Flow:
public static AsyncLocal<string> CorrelationId = new();
async Task ProcessRequestAsync()
{
CorrelationId.Value = Guid.NewGuid().ToString(); // Flows automatically across all awaited child tasks!
await StepOneAsync();
await StepTwoAsync();
}AsyncLocal<T> travels with the logical ExecutionContext, ensuring telemetry correlation IDs flow across thread pool thread transitions!