diff --git a/Elementary/Audio/PlaybackQueue.cs b/Elementary/Audio/PlaybackQueue.cs index cf1fc24..06359e8 100644 --- a/Elementary/Audio/PlaybackQueue.cs +++ b/Elementary/Audio/PlaybackQueue.cs @@ -1,6 +1,7 @@ using System.Collections.Concurrent; using Discord.Audio; using NAudio.Wave; +using NLog; namespace Elementary.Audio; @@ -30,12 +31,15 @@ public class PlaybackQueue private bool _isPlaying; + private ILogger _logger; + public PlaybackQueue(AudioManager audioManager) { _queue = new(); _isPlaying = false; _lock = new(); _audioManager = audioManager; + _logger = LogManager.GetCurrentClassLogger(); } /// @@ -45,14 +49,14 @@ public class PlaybackQueue public async Task Enqueue(PlaybackJob job) { _queue.Enqueue(job); - Console.WriteLine("Enqueued"); + _logger.Info("Enqueued"); if (!_isPlaying) { lock (_lock) { if (!_isPlaying) { - Console.WriteLine("Start Playing due to empty queue"); + _logger.Info("Start Playing due to empty queue"); PlayNext(); } } @@ -64,14 +68,14 @@ public class PlaybackQueue public void Flush() { _queue.Clear(); - Console.WriteLine("Queue Flushed"); + _logger.Info("Queue Flushed"); } private async void PlayNext() { if (_queue.TryDequeue(out var currentStream)) { - Console.WriteLine("Start Playing"); + _logger.Info("Start Playing"); _isPlaying = true; await (currentStream.Type switch @@ -80,14 +84,14 @@ public class PlaybackQueue JobType.Text => _audioManager.PlayText(currentStream.Text), _ => throw new ArgumentOutOfRangeException() }); - Console.WriteLine("Finished Playing"); + _logger.Info("Finished Playing"); await Task.Delay(200); _isPlaying = false; PlayNext(); } else if (_queue.IsEmpty) { - Console.WriteLine("Queue is empty"); + _logger.Info("Queue is empty"); _isPlaying = false; } } diff --git a/Elementary/Audio/SozaiAPI.cs b/Elementary/Audio/SozaiAPI.cs index 9978f8a..a671a27 100644 --- a/Elementary/Audio/SozaiAPI.cs +++ b/Elementary/Audio/SozaiAPI.cs @@ -1,10 +1,13 @@ using System.Text.Json; +using NLog; namespace Elementary.Audio; public class SozaiAPI { - private Uri AssetsInfoUrl = new("https://synchthia-sounds.storage.googleapis.com/index.json"); + private ILogger _logger; + + private Uri AssetsInfoUrl = new("https://r2.sim1222.com/sozai/index.json"); private Asset[] Assets; private class Asset @@ -26,6 +29,7 @@ public class SozaiAPI public SozaiAPI() { _client = new HttpClient(); + _logger = LogManager.GetCurrentClassLogger(); } public async Task Setup() @@ -34,7 +38,8 @@ public class SozaiAPI var stream = await response.Content.ReadAsStreamAsync(); Assets = await JsonSerializer.DeserializeAsync(stream); - Console.WriteLine($"Loaded {Assets.Length} assets"); + // Console.WriteLine($"Loaded {Assets.Length} assets"); + _logger.Info($"Loaded {Assets.Length} assets"); } public async Task GetAudioStream(string name) diff --git a/Elementary/Audio/VoicevoxAPI.cs b/Elementary/Audio/VoicevoxAPI.cs index 4fb32ad..01e4c5e 100644 --- a/Elementary/Audio/VoicevoxAPI.cs +++ b/Elementary/Audio/VoicevoxAPI.cs @@ -1,5 +1,6 @@ using System.Net.Http.Json; using System.Text; +using NLog; namespace Elementary.Audio; @@ -7,6 +8,7 @@ public class VoicevoxAPI { private UriBuilder _APIRootUrl; private HttpClient _client; + private ILogger _logger; public async Task Setup(string url) { @@ -14,6 +16,7 @@ public class VoicevoxAPI _APIRootUrl = new UriBuilder($"{_url.Scheme}://{_url.Host}:{_url.Port}"); _client = new HttpClient(); + _logger = LogManager.GetCurrentClassLogger(); } /// @@ -24,7 +27,7 @@ public class VoicevoxAPI /// public async Task Speak(string text, string speaker = "47") { - Console.WriteLine($"Requested TTS {text}"); + _logger.Info($"Requested TTS {text}"); var query = await GetAudioQuery(text, speaker); if (query == null) return null; diff --git a/Elementary/Commands/Handler.cs b/Elementary/Commands/Handler.cs index 0f03afe..e0cbf40 100644 --- a/Elementary/Commands/Handler.cs +++ b/Elementary/Commands/Handler.cs @@ -3,6 +3,7 @@ using Discord.Audio; using Discord.Commands; using Discord.WebSocket; using Elementary.Audio; +using NLog; namespace Elementary.Commands; @@ -13,6 +14,7 @@ public class Handler private readonly IServiceProvider _services; private readonly AudioManager _audioManager; private readonly MessageHandler _messageHandler; + private ILogger _logger; // Retrieve client and CommandService instance via ctor public Handler(DiscordSocketClient client, CommandService commands, IServiceProvider services, @@ -23,6 +25,7 @@ public class Handler _services = services; _audioManager = audioManager; _messageHandler = messageHandler; + _logger = LogManager.GetCurrentClassLogger(); } public async Task Setup() @@ -52,7 +55,7 @@ public class Handler if (_audioManager.isConnected) { - Console.WriteLine("Handling message"); + _logger.Info("Handling message"); await _messageHandler.HandleMessage(message); } } diff --git a/Elementary/Config/Configuration.cs b/Elementary/Config/Configuration.cs index c11915b..6b108a2 100644 --- a/Elementary/Config/Configuration.cs +++ b/Elementary/Config/Configuration.cs @@ -17,15 +17,16 @@ public class DiscordSettings public class Configuration { public AppSettings AppSettings { get; set; } + public IConfigurationRoot ConfigurationRoot { get; set; } - public static Configuration LoadFromJson() + public Configuration() { var builder = new ConfigurationBuilder() - .AddJsonFile("appsettings.json", false, true); - var configuration = builder.Build(); + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", false, true) + .Build(); - var config = new Configuration(); - configuration.Bind(config); - return config; + builder.Bind(this); + ConfigurationRoot = builder; } } \ No newline at end of file diff --git a/Elementary/Elementary.csproj b/Elementary/Elementary.csproj index e84cb02..b2edd8c 100644 --- a/Elementary/Elementary.csproj +++ b/Elementary/Elementary.csproj @@ -28,6 +28,8 @@ + + @@ -47,6 +49,9 @@ Always + + Always + diff --git a/Elementary/Program.cs b/Elementary/Program.cs index cbc2283..15101a9 100644 --- a/Elementary/Program.cs +++ b/Elementary/Program.cs @@ -8,13 +8,19 @@ using Elementary.Audio; using Elementary.Commands; using ManagedBass; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using NAudio.Wave; +using NLog; +using NLog.Extensions.Logging; using Configuration = Elementary.Config.Configuration; +using ILogger = NLog.ILogger; +using LogLevel = NLog.LogLevel; namespace Elementary; public class Program { + private ILogger _logger; private DiscordSocketClient _client; private IServiceProvider _services; private CommandService _commands; @@ -29,13 +35,19 @@ public class Program private async Task Setup() { - var configuration = Configuration.LoadFromJson(); + var configuration = new Configuration(); _client = new DiscordSocketClient(new DiscordSocketConfig { GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent }); _commands = new CommandService(); _services = new ServiceCollection() + .AddLogging(builder => + { + builder.ClearProviders(); + builder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); + builder.AddNLog(configuration.ConfigurationRoot); + }) .AddSingleton(_client) .AddSingleton(_commands) .AddSingleton() @@ -47,6 +59,8 @@ public class Program .AddSingleton() .BuildServiceProvider(); + _logger = LogManager.GetCurrentClassLogger(); + _sozaiAPI = _services.GetRequiredService(); _voicevoxAPI = _services.GetRequiredService(); @@ -68,9 +82,9 @@ public class Program await Task.Delay(-1); //keep the program running } - private static Task Log(LogMessage msg) + private Task Log(LogMessage msg) { - Console.WriteLine(msg.ToString()); + _logger.Log(LogLevel.FromString(msg.Severity.ToString()), msg.Exception, $"{msg.Source}: {msg.Message}"); return Task.CompletedTask; } } \ No newline at end of file diff --git a/Elementary/nlog.config b/Elementary/nlog.config new file mode 100644 index 0000000..c1bdf66 --- /dev/null +++ b/Elementary/nlog.config @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file