From 516e768aba1618191449f3ef5fb0e6253777e18b Mon Sep 17 00:00:00 2001 From: Toastie Date: Tue, 7 Jan 2025 19:06:33 +1300 Subject: [PATCH] Fixed error when flushing all logs --- EllieHub/Features/BotConfig/Services/LogWriter.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/EllieHub/Features/BotConfig/Services/LogWriter.cs b/EllieHub/Features/BotConfig/Services/LogWriter.cs index 8361d47..7a04e75 100644 --- a/EllieHub/Features/BotConfig/Services/LogWriter.cs +++ b/EllieHub/Features/BotConfig/Services/LogWriter.cs @@ -1,6 +1,7 @@ using EllieHub.Features.AppConfig.Models; using EllieHub.Features.BotConfig.Models; using EllieHub.Features.BotConfig.Services.Abstractions; +using System.Collections.Concurrent; using System.Diagnostics.CodeAnalysis; using System.Text; @@ -11,7 +12,7 @@ namespace EllieHub.Features.BotConfig.Services; /// public sealed class LogWriter : ILogWriter { - private readonly Dictionary _botLogs = []; + private readonly ConcurrentDictionary _botLogs = []; private readonly ReadOnlyAppSettings _appConfig; /// @@ -27,7 +28,11 @@ public sealed class LogWriter : ILogWriter /// public async Task FlushAllAsync(bool removeFromMemory = false, CancellationToken cToken = default) { - var result = await Task.WhenAll(_botLogs.Keys.Select(x => FlushAsync(x, removeFromMemory, cToken))); + var result = await Task.WhenAll(_botLogs.Keys.Select(x => FlushAsync(x, false, cToken))); + + if (removeFromMemory) + _botLogs.Clear(); + return result.Any(x => x); } @@ -38,7 +43,7 @@ public sealed class LogWriter : ILogWriter return false; if (removeFromMemory) - _botLogs.Remove(botId); + _botLogs.Remove(botId, out _); if (logStringBuilder.Length is 0) return false;