【发布时间】:2015-04-30 10:12:51
【问题描述】:
Trace.CorrelationManager.LogicalOperationStack 支持嵌套逻辑操作标识符,其中最常见的情况是日志记录 (NDC)。它还应该与async-await 一起使用吗?
这是一个使用LogicalFlow 的简单示例,这是我对LogicalOperationStack 的简单包装:
private static void Main() => OuterOperationAsync().GetAwaiter().GetResult();
private static async Task OuterOperationAsync()
{
Console.WriteLine(LogicalFlow.CurrentOperationId);
using (LogicalFlow.StartScope())
{
Console.WriteLine("\t" + LogicalFlow.CurrentOperationId);
await InnerOperationAsync();
Console.WriteLine("\t" + LogicalFlow.CurrentOperationId);
await InnerOperationAsync();
Console.WriteLine("\t" + LogicalFlow.CurrentOperationId);
}
Console.WriteLine(LogicalFlow.CurrentOperationId);
}
private static async Task InnerOperationAsync()
{
using (LogicalFlow.StartScope())
{
await Task.Delay(100);
}
}
LogicalFlow:
public static class LogicalFlow
{
public static Guid CurrentOperationId =>
Trace.CorrelationManager.LogicalOperationStack.Count > 0
? (Guid) Trace.CorrelationManager.LogicalOperationStack.Peek()
: Guid.Empty;
public static IDisposable StartScope()
{
Trace.CorrelationManager.StartLogicalOperation();
return new Stopper();
}
private static void StopScope() =>
Trace.CorrelationManager.StopLogicalOperation();
private class Stopper : IDisposable
{
private bool _isDisposed;
public void Dispose()
{
if (!_isDisposed)
{
StopScope();
_isDisposed = true;
}
}
}
}
输出:
00000000-0000-0000-0000-000000000000
49985135-1e39-404c-834a-9f12026d9b65
54674452-e1c5-4b1b-91ed-6bd6ea725b98
c6ec00fd-bff8-4bde-bf70-e073b6714ae5
54674452-e1c5-4b1b-91ed-6bd6ea725b98
具体的值并不重要,但据我了解,外线应显示Guid.Empty(即00000000-0000-0000-0000-000000000000),内线应显示相同的Guid值。
您可能会说LogicalOperationStack 使用了Stack,它不是线程安全的,这就是输出错误的原因。但是虽然这通常是正确的,但在这种情况下只有一个线程同时访问LogicalOperationStack(每个async 操作在调用时都会等待,并且不使用组合符,例如Task.WhenAll)
问题在于LogicalOperationStack 存储在具有写入时复制行为的CallContext 中。这意味着只要您没有在CallContext 中显式设置某些内容(并且当您使用StartLogicalOperation 添加到现有堆栈时也没有),您使用的是父上下文而不是您自己的上下文。
这可以通过在添加到现有堆栈之前简单地将 anything 设置到 CallContext 来显示。例如,如果我们将StartScope 更改为:
public static IDisposable StartScope()
{
CallContext.LogicalSetData("Bar", "Arnon");
Trace.CorrelationManager.StartLogicalOperation();
return new Stopper();
}
输出是:
00000000-0000-0000-0000-000000000000
fdc22318-53ef-4ae5-83ff-6c3e3864e37a
fdc22318-53ef-4ae5-83ff-6c3e3864e37a
fdc22318-53ef-4ae5-83ff-6c3e3864e37a
00000000-0000-0000-0000-000000000000
注意:我并不是建议任何人实际这样做。真正实用的解决方案是使用ImmutableStack 而不是LogicalOperationStack,因为它既是线程安全的,又因为当您调用Pop 时它是不可变的,您会得到一个新的ImmutableStack,然后您需要将其设置回CallContext。完整的实现可以作为这个问题的答案:Tracking c#/.NET tasks flow
那么,LogicalOperationStack 是否应该与 async 一起使用,而这只是一个错误? LogicalOperationStack 不适合 async 世界吗?还是我错过了什么?
更新:使用Task.Delay 显然令人困惑,因为它使用System.Threading.Timer 而captures the ExecutionContext internally。使用await Task.Yield(); 而不是await Task.Delay(100); 使示例更易于理解。
【问题讨论】:
-
@Noseratio 不。这不是关于
async-await,而是关于一个已经修复的错误。 -
同意,不是骗子。您是否检查过它是否在同一线程上按预期工作,例如如果您将
await Task.Delay(100)替换为Task.Delay(100).Wait()? -
@downvoter 有什么问题吗?
-
这是一个很棒的技术问题。也许,投反对票的人根本不明白这是怎么回事。
标签: c# .net async-await .net-4.5