From 4d7f6675ad8256a9314824eff4e4c1fba5a037ef Mon Sep 17 00:00:00 2001 From: Kaoticz <1812311+Kaoticz@users.noreply.github.com> Date: Wed, 1 Jan 2025 02:34:43 -0300 Subject: [PATCH] Fixed error when flushing all logs --- NadekoHub/Features/BotConfig/Services/LogWriter.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/NadekoHub/Features/BotConfig/Services/LogWriter.cs b/NadekoHub/Features/BotConfig/Services/LogWriter.cs index eb03766..cec515d 100644 --- a/NadekoHub/Features/BotConfig/Services/LogWriter.cs +++ b/NadekoHub/Features/BotConfig/Services/LogWriter.cs @@ -1,6 +1,7 @@ using NadekoHub.Features.AppConfig.Models; using NadekoHub.Features.BotConfig.Models; using NadekoHub.Features.BotConfig.Services.Abstractions; +using System.Collections.Concurrent; using System.Diagnostics.CodeAnalysis; using System.Text; @@ -11,7 +12,7 @@ namespace NadekoHub.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 LogWriter(ReadOnlyAppSettings appConfig) /// 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 async Task FlushAsync(Guid botId, bool removeFromMemory = false, Ca return false; if (removeFromMemory) - _botLogs.Remove(botId); + _botLogs.Remove(botId, out _); if (logStringBuilder.Length is 0) return false;