This commit is contained in:
2026-01-31 15:13:17 +01:00
parent fe091cd296
commit 4b69b217e0
3 changed files with 23 additions and 22 deletions

View File

@@ -120,6 +120,14 @@ namespace EonaCat.Logger.EonaCatCoreLogger
catch (OperationCanceledException) { } catch (OperationCanceledException) { }
} }
public void Flush()
{
while (_queue.Count > 0)
{
Thread.Sleep(100);
}
}
public void Dispose() public void Dispose()
{ {
_cts.Cancel(); _cts.Cancel();

View File

@@ -7,8 +7,17 @@ namespace EonaCat.Logger.EonaCatCoreLogger
{ {
internal sealed class LoggerScopedContext internal sealed class LoggerScopedContext
{ {
private static readonly AsyncLocal<Stack<Dictionary<string, string>>> _scopes private static readonly AsyncLocal<Stack<Dictionary<string, string>>> _scopes = new();
= 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) public IDisposable BeginScope<TState>(TState state)
{ {
@@ -19,57 +28,41 @@ namespace EonaCat.Logger.EonaCatCoreLogger
public void Set(string key, string value) public void Set(string key, string value)
{ {
if (_scopes.Value == null || _scopes.Value.Count == 0) EnsureScope();
{
return;
}
_scopes.Value.Peek()[key] = value; _scopes.Value.Peek()[key] = value;
} }
public string Get(string key) public string Get(string key)
{ {
if (_scopes.Value == null) if (_scopes.Value == null)
{
return null; return null;
}
foreach (var scope in _scopes.Value) foreach (var scope in _scopes.Value)
{ {
if (scope.TryGetValue(key, out var value)) if (scope.TryGetValue(key, out var value))
{
return value; return value;
} }
}
return null; return null;
} }
public IReadOnlyDictionary<string, string> GetAll() public IReadOnlyDictionary<string, string> GetAll()
{ {
EnsureScope();
var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); 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 scope in _scopes.Value)
{ {
foreach (var kv in scope) foreach (var kv in scope)
{ {
if (!result.ContainsKey(kv.Key)) if (!result.ContainsKey(kv.Key))
{
result[kv.Key] = kv.Value; result[kv.Key] = kv.Value;
} }
} }
}
return result; return result;
} }
public void Clear() public void Clear()
{ {
_scopes.Value?.Clear(); _scopes.Value?.Clear();

View File

@@ -109,7 +109,7 @@
void LoggerSettings_OnLog(EonaCatLogMessage message) void LoggerSettings_OnLog(EonaCatLogMessage message)
{ {
Console.ForegroundColor = ConsoleColor.Yellow; Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"LogCounter: {++onLogCounter} {message}"); //Console.WriteLine($"LogCounter: {++onLogCounter} {message}");
Console.ForegroundColor = defaultColor; Console.ForegroundColor = defaultColor;
} }