From 6dd7de3cee349b9b39fc539c3c490a2606a5d179 Mon Sep 17 00:00:00 2001 From: Twinki Date: Fri, 26 Jan 2024 21:52:09 -0500 Subject: [PATCH] fix logger message --- readme.md | 2 +- .../Hosted/GuildEventMonitorService.cs | 21 +++++++------ .../Hosted/GuildEventScheduleService.cs | 31 ++++++++++--------- src/Miha.sln | 1 + 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/readme.md b/readme.md index 782f4cb..c400826 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ # Miha, a discord bot for Midnight Haven -- .NET 7 based, Redis backed +- .NET 8 based, Redis backed - [Discord.NET based](https://github.com/discord-net/Discord.Net) - Built & structured around dependency injection - Rapidly integrated using GitHub Actions diff --git a/src/Miha.Discord/Services/Hosted/GuildEventMonitorService.cs b/src/Miha.Discord/Services/Hosted/GuildEventMonitorService.cs index af398c2..4f29708 100644 --- a/src/Miha.Discord/Services/Hosted/GuildEventMonitorService.cs +++ b/src/Miha.Discord/Services/Hosted/GuildEventMonitorService.cs @@ -22,6 +22,7 @@ public partial class GuildEventMonitorService( ILogger logger) : DiscordClientService(client, logger) { private readonly DiscordOptions _discordOptions = discordOptions.Value; + private readonly ILogger _logger = logger; private const string Schedule = "0,5,10,15,20,25,30,35,40,45,50,55 ? * * *"; // https://crontab.cronhub.io/ @@ -34,7 +35,7 @@ public partial class GuildEventMonitorService( protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - logger.LogInformation("Waiting for client to be ready..."); + _logger.LogInformation("Waiting for client to be ready..."); await Client.WaitForReadyAsync(stoppingToken); @@ -47,14 +48,14 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) if (nextUtc is null) { - logger.LogWarning("Next utc occurence is null"); + _logger.LogWarning("Next utc occurence is null"); await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken); continue; } var next = nextUtc.Value - utcNow; - logger.LogDebug("Waiting {Time} until next operation", next.Humanize(3)); + _logger.LogDebug("Waiting {Time} until next operation", next.Humanize(3)); await Task.Delay(nextUtc.Value - utcNow, stoppingToken); } @@ -69,7 +70,7 @@ private async Task CheckScheduledEventsAsync() guild = Client.GetGuild(_discordOptions.Guild!.Value); if (guild is null) { - logger.LogCritical("Guild is null {GuildId}", _discordOptions.Guild.Value); + _logger.LogCritical("Guild is null {GuildId}", _discordOptions.Guild.Value); return; } } @@ -88,11 +89,11 @@ private async Task CheckScheduledEventsAsync() { if (announcementChannel.Reasons.Any(m => m.Message == "Announcement channel not set")) { - logger.LogDebug("Guild announcement channel not set {GuildId} {EventId}", guildEvent.Guild.Id, guildEvent.Id); + _logger.LogDebug("Guild announcement channel not set {GuildId} {EventId}", guildEvent.Guild.Id, guildEvent.Id); return; } - logger.LogInformation("Failed getting announcement channel for guild {GuildId} {EventId}", guild.Id, guildEvent.Id); + _logger.LogInformation("Failed getting announcement channel for guild {GuildId} {EventId}", guild.Id, guildEvent.Id); continue; } @@ -104,17 +105,17 @@ private async Task CheckScheduledEventsAsync() startsIn = new TimeSpan(startsIn.Days, startsIn.Hours, startsIn.Minutes + 1, 0); } - logger.LogDebug("GuildEvent {EventId} starts in (rounded) {StartsInTotalMinutes}", guildEvent.Id, startsIn.TotalMinutes); + _logger.LogDebug("GuildEvent {EventId} starts in (rounded) {StartsInTotalMinutes}", guildEvent.Id, startsIn.TotalMinutes); if (startsIn.TotalMinutes is < 5 or > 20 || _memoryCache.TryGetValue(guildEvent.Id, out bool notified) && notified) { - logger.LogDebug("GuildEvent {EventId} starts too soon or is already notified", guildEvent.Id); + _logger.LogDebug("GuildEvent {EventId} starts too soon or is already notified", guildEvent.Id); continue; } - if (logger.IsEnabled(LogLevel.Debug)) + if (_logger.IsEnabled(LogLevel.Debug)) { - logger.LogDebug("GuildEvent {EventId} {GuildEventJson}", guildEvent.Id, JsonConvert.SerializeObject(new + _logger.LogDebug("GuildEvent {EventId} {GuildEventJson}", guildEvent.Id, JsonConvert.SerializeObject(new { guildEvent.StartTime, guildEvent.Id, diff --git a/src/Miha.Discord/Services/Hosted/GuildEventScheduleService.cs b/src/Miha.Discord/Services/Hosted/GuildEventScheduleService.cs index ff34f15..61e21c2 100644 --- a/src/Miha.Discord/Services/Hosted/GuildEventScheduleService.cs +++ b/src/Miha.Discord/Services/Hosted/GuildEventScheduleService.cs @@ -26,6 +26,7 @@ public partial class GuildEventScheduleService( { private readonly DiscordSocketClient _client = client; private readonly DiscordOptions _discordOptions = discordOptions.Value; + private readonly ILogger _logger = logger; private const string Schedule = "0,5,10,15,20,25,30,35,40,45,50,55 * * * *"; // https://crontab.cronhub.io/ @@ -33,7 +34,7 @@ public partial class GuildEventScheduleService( protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - logger.LogInformation("Waiting for client to be ready..."); + _logger.LogInformation("Waiting for client to be ready..."); await Client.WaitForReadyAsync(stoppingToken); @@ -45,7 +46,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) } catch (HttpException e) { - logger.LogWarning(e, "Discord dotnet http exception caught, likely caused by rate-limits, waiting a few minutes before continuing"); + _logger.LogWarning(e, "Discord dotnet http exception caught, likely caused by rate-limits, waiting a few minutes before continuing"); await Task.Delay(TimeSpan.FromMinutes(3), stoppingToken); @@ -57,26 +58,26 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) if (nextUtc is null) { - logger.LogWarning("Next utc occurence is null"); + _logger.LogWarning("Next utc occurence is null"); await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken); continue; } var next = nextUtc.Value - utcNow; - logger.LogDebug("Waiting {Time} until next operation", next.Humanize(3)); + _logger.LogDebug("Waiting {Time} until next operation", next.Humanize(3)); await Task.Delay(nextUtc.Value - utcNow, stoppingToken); } - logger.LogInformation("Hosted service ended"); + _logger.LogInformation("Hosted service ended"); } private async Task PostWeeklyScheduleAsync() { if (_discordOptions.Guild is null) { - logger.LogWarning("Guild isn't configured"); + _logger.LogWarning("Guild isn't configured"); return; } @@ -85,13 +86,13 @@ private async Task PostWeeklyScheduleAsync() if (guildResult.IsFailed || guild is null) { - logger.LogWarning("Guild doc failed, or the guild is null for some reason {Errors}", guildResult.Errors); + _logger.LogWarning("Guild doc failed, or the guild is null for some reason {Errors}", guildResult.Errors); return; } if (guild.WeeklyScheduleChannel is null) { - logger.LogDebug("Guild doesn't have a configured weekly schedule channel"); + _logger.LogDebug("Guild doesn't have a configured weekly schedule channel"); return; } @@ -100,7 +101,7 @@ private async Task PostWeeklyScheduleAsync() if (eventsThisWeekResult.IsFailed || eventsThisWeek is null) { - logger.LogWarning("Fetching this weeks events failed, or is null {Errors}", eventsThisWeekResult.Errors); + _logger.LogWarning("Fetching this weeks events failed, or is null {Errors}", eventsThisWeekResult.Errors); return; } @@ -109,7 +110,7 @@ private async Task PostWeeklyScheduleAsync() if (weeklyScheduleChannelResult.IsFailed || weeklyScheduleChannel is null) { - logger.LogWarning("Fetching the guilds weekly schedule channel failed, or is null {Errors}", weeklyScheduleChannelResult.Errors); + _logger.LogWarning("Fetching the guilds weekly schedule channel failed, or is null {Errors}", weeklyScheduleChannelResult.Errors); return; } @@ -127,7 +128,7 @@ private async Task PostWeeklyScheduleAsync() } } - logger.LogInformation("Updating weekly schedule"); + _logger.LogInformation("Updating weekly schedule"); var postedHeader = false; var postedFooter = false; @@ -158,7 +159,7 @@ private async Task PostWeeklyScheduleAsync() { var deletedMessages = 0; - logger.LogInformation("Wiping posted messages"); + _logger.LogInformation("Wiping posted messages"); foreach (var message in messagesToDelete) { @@ -166,7 +167,7 @@ private async Task PostWeeklyScheduleAsync() deletedMessages++; } - logger.LogInformation("Deleted {DeletedMessages} messages", deletedMessages); + _logger.LogInformation("Deleted {DeletedMessages} messages", deletedMessages); // Update the messages list messages = (await weeklyScheduleChannel @@ -268,7 +269,7 @@ private async Task PostWeeklyScheduleAsync() if (lastPostedMessage is null) { - logger.LogInformation("Posting new message"); + _logger.LogInformation("Posting new message"); await weeklyScheduleChannel.SendMessageAsync(embed: embed.Build()); } else @@ -280,7 +281,7 @@ await weeklyScheduleChannel.ModifyMessageAsync(lastPostedMessage.Id, props => } } - logger.LogInformation("Finished updating weekly schedule"); + _logger.LogInformation("Finished updating weekly schedule"); } [LoggerMessage(EventId = 1, Level = LogLevel.Error, Message = "Exception occurred")] diff --git a/src/Miha.sln b/src/Miha.sln index 7d8430b..78a806e 100644 --- a/src/Miha.sln +++ b/src/Miha.sln @@ -12,6 +12,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Dockerfile = Dockerfile ..\docker-compose.dev.yml = ..\docker-compose.dev.yml appsettings.edge.json = appsettings.edge.json + ..\readme.md = ..\readme.md EndProjectSection EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Miha.Redis", "Miha.Redis\Miha.Redis.csproj", "{51E2A7AC-9B3D-4453-89E7-EF81DAE37E41}"