LoggerExternalScopeProvider
Default implementation of IExternalScopeProvider
using System;
using System.Runtime.CompilerServices;
using System.Threading;
namespace Microsoft.Extensions.Logging
{
public class LoggerExternalScopeProvider : IExternalScopeProvider
{
private sealed class Scope : IDisposable
{
private readonly LoggerExternalScopeProvider _provider;
private bool _isDisposed;
public Scope Parent { get; }
public object State { get; }
internal Scope(LoggerExternalScopeProvider provider, object state, Scope parent)
{
_provider = provider;
State = state;
Parent = parent;
}
public override string ToString()
{
return State?.ToString();
}
public void Dispose()
{
if (!_isDisposed) {
_provider._currentScope.Value = Parent;
_isDisposed = true;
}
}
}
private readonly AsyncLocal<Scope> _currentScope = new AsyncLocal<Scope>();
[System.Runtime.CompilerServices.NullableContext(1)]
public void ForEachScope<[System.Runtime.CompilerServices.Nullable(2)] TState>([System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2,
1
})] Action<object, TState> callback, TState state)
{
<>c__DisplayClass2_0<TState> <>c__DisplayClass2_ = default(<>c__DisplayClass2_0<TState>);
<>c__DisplayClass2_.callback = callback;
<>c__DisplayClass2_.state = state;
<ForEachScope>g__Report|2_0(_currentScope.Value, ref <>c__DisplayClass2_);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public IDisposable Push([System.Runtime.CompilerServices.Nullable(2)] object state)
{
Scope value = _currentScope.Value;
Scope scope = new Scope(this, state, value);
_currentScope.Value = scope;
return scope;
}
}
}