diff --git a/AzzyBot-Next/Logging/FileLogger.cs b/AzzyBot-Next/Logging/FileLogger.cs index 01e29a8d..434e8395 100644 --- a/AzzyBot-Next/Logging/FileLogger.cs +++ b/AzzyBot-Next/Logging/FileLogger.cs @@ -16,8 +16,10 @@ public sealed class FileLogger(string path) : ILogger, IDisposable public void Dispose(bool disposing) { - if (disposing) - _semaphore.Dispose(); + if (!disposing) + return; + + _semaphore.Dispose(); } public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) diff --git a/AzzyBot-Next/Logging/FileLoggerProvider.cs b/AzzyBot-Next/Logging/FileLoggerProvider.cs index fea4a106..337ae162 100644 --- a/AzzyBot-Next/Logging/FileLoggerProvider.cs +++ b/AzzyBot-Next/Logging/FileLoggerProvider.cs @@ -10,9 +10,13 @@ public sealed class FileLoggerProvider(string filePath) : ILoggerProvider private readonly ConcurrentDictionary _loggers = new(); public ILogger CreateLogger(string categoryName) => _loggers.GetOrAdd(categoryName, name => new FileLogger(_filePath)); + public void Dispose() => Dispose(true); - public void Dispose() + public void Dispose(bool disposing) { + if (!disposing) + return; + foreach (KeyValuePair logger in _loggers) { logger.Value.Dispose(); diff --git a/AzzyBot-Next/Logging/LoggingBuilderExtensions.cs b/AzzyBot-Next/Logging/LoggingBuilderExtensions.cs index e522a437..a8d6d7bd 100644 --- a/AzzyBot-Next/Logging/LoggingBuilderExtensions.cs +++ b/AzzyBot-Next/Logging/LoggingBuilderExtensions.cs @@ -1,3 +1,6 @@ +using System; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Logging; namespace AzzyBot.Logging; @@ -6,9 +9,10 @@ public static class LoggingBuilderExtensions { public static ILoggingBuilder AddFile(this ILoggingBuilder builder, string filePath) { - using FileLoggerProvider fileLoggerProvider = new(filePath); - builder.AddProvider(fileLoggerProvider); + ArgumentNullException.ThrowIfNull(builder, nameof(builder)); + + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton(p => new FileLoggerProvider(filePath))); return builder; } -} \ No newline at end of file +}