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) { }
}
public void Flush()
{
while (_queue.Count > 0)
{
Thread.Sleep(100);
}
}
public void Dispose()
{
_cts.Cancel();

View File

@@ -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,57 +28,41 @@ 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;
}
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();

View File

@@ -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;
}