Updated
This commit is contained in:
@@ -120,6 +120,14 @@ namespace EonaCat.Logger.EonaCatCoreLogger
|
||||
catch (OperationCanceledException) { }
|
||||
}
|
||||
|
||||
public void Flush()
|
||||
{
|
||||
while (_queue.Count > 0)
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_cts.Cancel();
|
||||
|
||||
@@ -7,8 +7,17 @@ namespace EonaCat.Logger.EonaCatCoreLogger
|
||||
{
|
||||
internal sealed class LoggerScopedContext
|
||||
{
|
||||
private static readonly AsyncLocal<Stack<Dictionary<string, string>>> _scopes
|
||||
= new();
|
||||
private static readonly AsyncLocal<Stack<Dictionary<string, string>>> _scopes = new();
|
||||
|
||||
// Ensure there is always a scope to write into
|
||||
private void EnsureScope()
|
||||
{
|
||||
_scopes.Value ??= new Stack<Dictionary<string, string>>();
|
||||
if (_scopes.Value.Count == 0)
|
||||
{
|
||||
_scopes.Value.Push(new Dictionary<string, string>());
|
||||
}
|
||||
}
|
||||
|
||||
public IDisposable BeginScope<TState>(TState state)
|
||||
{
|
||||
@@ -19,27 +28,19 @@ namespace EonaCat.Logger.EonaCatCoreLogger
|
||||
|
||||
public void Set(string key, string value)
|
||||
{
|
||||
if (_scopes.Value == null || _scopes.Value.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
EnsureScope();
|
||||
_scopes.Value.Peek()[key] = value;
|
||||
}
|
||||
|
||||
public string Get(string key)
|
||||
{
|
||||
if (_scopes.Value == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach (var scope in _scopes.Value)
|
||||
{
|
||||
if (scope.TryGetValue(key, out var value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -47,29 +48,21 @@ namespace EonaCat.Logger.EonaCatCoreLogger
|
||||
|
||||
public IReadOnlyDictionary<string, string> GetAll()
|
||||
{
|
||||
EnsureScope();
|
||||
|
||||
var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
if (_scopes.Value == null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
// Iterate from top of stack to bottom
|
||||
foreach (var scope in _scopes.Value)
|
||||
{
|
||||
foreach (var kv in scope)
|
||||
{
|
||||
if (!result.ContainsKey(kv.Key))
|
||||
{
|
||||
result[kv.Key] = kv.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_scopes.Value?.Clear();
|
||||
|
||||
@@ -109,7 +109,7 @@
|
||||
void LoggerSettings_OnLog(EonaCatLogMessage message)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
Console.WriteLine($"LogCounter: {++onLogCounter} {message}");
|
||||
//Console.WriteLine($"LogCounter: {++onLogCounter} {message}");
|
||||
Console.ForegroundColor = defaultColor;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user