Skip to content

Commit

Permalink
Improve some basics
Browse files Browse the repository at this point in the history
  • Loading branch information
Sella-GH committed Jun 4, 2024
1 parent a9562f1 commit a9ddc19
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
14 changes: 7 additions & 7 deletions AzzyBot-Next/Logging/FileLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@

namespace AzzyBot.Logging;

public sealed class FileLogger(string filePath) : ILogger, IDisposable
public sealed class FileLogger(string path) : ILogger, IDisposable
{
private readonly string _filePath = filePath;
private readonly string _path = path;
private readonly SemaphoreSlim _semaphore = new(1, 1);

#pragma warning disable CS8633 // Nullability in constraints for type parameter doesn't match the constraints for type parameter in implicitly implemented interface method'.
public IDisposable? BeginScope<TState>(TState? state) => null;
#pragma warning restore CS8633 // Nullability in constraints for type parameter doesn't match the constraints for type parameter in implicitly implemented interface method'.

public IDisposable? BeginScope<TState>(TState state) where TState : notnull => default!;
public bool IsEnabled(LogLevel logLevel) => true;
public void Dispose() => Dispose(true);

Expand All @@ -27,6 +24,9 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
{
ArgumentNullException.ThrowIfNull(formatter, nameof(formatter));

if (!IsEnabled(logLevel))
return;

_semaphore.Wait();

string message = formatter(state, exception);
Expand All @@ -40,7 +40,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except

try
{
File.AppendAllText(_filePath, logMessage);
File.AppendAllText(_path, logMessage);
}
finally
{
Expand Down
7 changes: 2 additions & 5 deletions AzzyBot-Next/Logging/FileLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ public sealed class FileLoggerProvider(string filePath) : ILoggerProvider
private readonly string _filePath = filePath;
private readonly ConcurrentDictionary<string, FileLogger> _loggers = new();

public ILogger CreateLogger(string categoryName)
{
return _loggers.GetOrAdd(categoryName, name => new FileLogger(_filePath));
}
public ILogger CreateLogger(string categoryName) => _loggers.GetOrAdd(categoryName, name => new FileLogger(_filePath));

public void Dispose()
{
Expand All @@ -23,4 +20,4 @@ public void Dispose()

_loggers.Clear();
}
}
}

0 comments on commit a9ddc19

Please sign in to comment.