From 4b69b217e098df85a6a65c4d50704410276f263e Mon Sep 17 00:00:00 2001 From: EonaCat Date: Sat, 31 Jan 2026 15:13:17 +0100 Subject: [PATCH] Updated --- .../EonaCatCoreLogger/JsonFileLogger.cs | 8 +++++ .../EonaCatCoreLogger/LogContext.cs | 35 ++++++++----------- Testers/EonaCat.Logger.Test.Web/Program.cs | 2 +- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/EonaCat.Logger/EonaCatCoreLogger/JsonFileLogger.cs b/EonaCat.Logger/EonaCatCoreLogger/JsonFileLogger.cs index 15098f9..07f8391 100644 --- a/EonaCat.Logger/EonaCatCoreLogger/JsonFileLogger.cs +++ b/EonaCat.Logger/EonaCatCoreLogger/JsonFileLogger.cs @@ -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(); diff --git a/EonaCat.Logger/EonaCatCoreLogger/LogContext.cs b/EonaCat.Logger/EonaCatCoreLogger/LogContext.cs index ed11c05..85c186f 100644 --- a/EonaCat.Logger/EonaCatCoreLogger/LogContext.cs +++ b/EonaCat.Logger/EonaCatCoreLogger/LogContext.cs @@ -7,8 +7,17 @@ namespace EonaCat.Logger.EonaCatCoreLogger { internal sealed class LoggerScopedContext { - private static readonly AsyncLocal>> _scopes - = new(); + private static readonly AsyncLocal>> _scopes = new(); + + // Ensure there is always a scope to write into + private void EnsureScope() + { + _scopes.Value ??= new Stack>(); + if (_scopes.Value.Count == 0) + { + _scopes.Value.Push(new Dictionary()); + } + } public IDisposable BeginScope(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 GetAll() { + EnsureScope(); + var result = new Dictionary(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(); diff --git a/Testers/EonaCat.Logger.Test.Web/Program.cs b/Testers/EonaCat.Logger.Test.Web/Program.cs index 20d8201..c9c4036 100644 --- a/Testers/EonaCat.Logger.Test.Web/Program.cs +++ b/Testers/EonaCat.Logger.Test.Web/Program.cs @@ -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; }