Skip to content

Commit

Permalink
Improve the basics again
Browse files Browse the repository at this point in the history
  • Loading branch information
Sella-GH committed Jun 4, 2024
1 parent a9ddc19 commit c140718
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
6 changes: 4 additions & 2 deletions AzzyBot-Next/Logging/FileLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
Expand Down
6 changes: 5 additions & 1 deletion AzzyBot-Next/Logging/FileLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ public sealed class FileLoggerProvider(string filePath) : ILoggerProvider
private readonly ConcurrentDictionary<string, FileLogger> _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<string, FileLogger> logger in _loggers)
{
logger.Value.Dispose();
Expand Down
10 changes: 7 additions & 3 deletions AzzyBot-Next/Logging/LoggingBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;

namespace AzzyBot.Logging;
Expand All @@ -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<ILoggerProvider, FileLoggerProvider>(p => new FileLoggerProvider(filePath)));

return builder;
}
}
}

0 comments on commit c140718

Please sign in to comment.