This repository was archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 245
/
Copy pathLoggerExternalScopeProvider.cs
76 lines (65 loc) · 2.13 KB
/
LoggerExternalScopeProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.Extensions.Logging;
namespace Microsoft.Extensions.Logging
{
/// <summary>
/// Default implemenation of <see cref="IExternalScopeProvider"/>
/// </summary>
public class LoggerExternalScopeProvider : IExternalScopeProvider
{
private readonly AsyncLocal<Scope> _currentScope = new AsyncLocal<Scope>();
/// <inheritdoc />
public void ForEachScope<TState>(Action<object, TState> callback, TState state)
{
void Report(Scope current)
{
if (current == null)
{
return;
}
Report(current.Parent);
callback(current.State, state);
}
Report(_currentScope.Value);
}
/// <inheritdoc />
public IDisposable Push(object state)
{
var parent = _currentScope.Value;
var newScope = new Scope(this, state, parent);
_currentScope.Value = newScope;
return newScope;
}
private class Scope : IDisposable
{
private readonly LoggerExternalScopeProvider _provider;
private bool _isDisposed;
internal Scope(LoggerExternalScopeProvider provider, object state, Scope parent)
{
_provider = provider;
State = state;
Parent = parent;
}
public Scope Parent { get; }
public object State { get; }
public override string ToString()
{
return State?.ToString();
}
public void Dispose()
{
if (!_isDisposed)
{
_provider._currentScope.Value = Parent;
_isDisposed = true;
}
}
}
}
}