Fixed error when flushing all logs

This commit is contained in:
Toastie 2025-01-07 19:06:33 +13:00
parent f0030af0ae
commit 516e768aba
Signed by: toastie_t0ast
GPG key ID: 0861BE54AD481DC7

View file

@ -1,6 +1,7 @@
using EllieHub.Features.AppConfig.Models; using EllieHub.Features.AppConfig.Models;
using EllieHub.Features.BotConfig.Models; using EllieHub.Features.BotConfig.Models;
using EllieHub.Features.BotConfig.Services.Abstractions; using EllieHub.Features.BotConfig.Services.Abstractions;
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Text; using System.Text;
@ -11,7 +12,7 @@ namespace EllieHub.Features.BotConfig.Services;
/// </summary> /// </summary>
public sealed class LogWriter : ILogWriter public sealed class LogWriter : ILogWriter
{ {
private readonly Dictionary<Guid, StringBuilder> _botLogs = []; private readonly ConcurrentDictionary<Guid, StringBuilder> _botLogs = [];
private readonly ReadOnlyAppSettings _appConfig; private readonly ReadOnlyAppSettings _appConfig;
/// <inheritdoc/> /// <inheritdoc/>
@ -27,7 +28,11 @@ public sealed class LogWriter : ILogWriter
/// <inheritdoc/> /// <inheritdoc/>
public async Task<bool> FlushAllAsync(bool removeFromMemory = false, CancellationToken cToken = default) public async Task<bool> 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); return result.Any(x => x);
} }
@ -38,7 +43,7 @@ public sealed class LogWriter : ILogWriter
return false; return false;
if (removeFromMemory) if (removeFromMemory)
_botLogs.Remove(botId); _botLogs.Remove(botId, out _);
if (logStringBuilder.Length is 0) if (logStringBuilder.Length is 0)
return false; return false;