From 4ba034f9889e998f0aac6ed9722770dbf7a3b431 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Sat, 5 Jul 2025 15:39:37 +0200 Subject: [PATCH] chore(deps): update dependency nlog.web.aspnetcore to v6 (#3438) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore(deps): update dependency nlog.web.aspnetcore to v6 * Adapt ASF to NLog v6 * Use another approach for NLog file archiving * Use cached ${currentdir} to improve performance * Misc fix * Update suffix format * Misc --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Ɓukasz Domeradzki --- ArchiSteamFarm/NLog/Logging.cs | 15 ++++++++++----- ArchiSteamFarm/NLog/Targets/HistoryTarget.cs | 4 +--- ArchiSteamFarm/NLog/Targets/SteamTarget.cs | 17 +++++++++++------ ArchiSteamFarm/Program.cs | 4 ++-- ArchiSteamFarm/SharedInfo.cs | 1 - Directory.Packages.props | 2 +- 6 files changed, 25 insertions(+), 18 deletions(-) diff --git a/ArchiSteamFarm/NLog/Logging.cs b/ArchiSteamFarm/NLog/Logging.cs index bf606f323..75e75c66c 100644 --- a/ArchiSteamFarm/NLog/Logging.cs +++ b/ArchiSteamFarm/NLog/Logging.cs @@ -207,6 +207,7 @@ internal static class Logging { internal static void InitCoreLoggers(bool uniqueInstance) { try { + // Handle edge case of user using NLog.config in non-standard directory (current directory) if ((Directory.GetCurrentDirectory() != AppContext.BaseDirectory) && File.Exists(NLogConfigurationFile)) { IsUsingCustomConfiguration = true; @@ -224,6 +225,11 @@ internal static class Logging { } if (uniqueInstance) { + if (LogManager.Configuration == null) { + // This should never happen, as configuration must be initialized by now (either by user's config, InitEmergencyLoggers() or InitCoreLoggers() above) + throw new InvalidOperationException(nameof(LogManager.Configuration)); + } + try { Directory.CreateDirectory(SharedInfo.ArchivalLogsDirectory); } catch (Exception e) { @@ -232,12 +238,10 @@ internal static class Logging { #pragma warning disable CA2000 // False positive, we're adding this disposable object to the global scope, so we can't dispose it FileTarget fileTarget = new("File") { - ArchiveFileName = Path.Combine("${currentdir}", SharedInfo.ArchivalLogsDirectory, SharedInfo.ArchivalLogFile), - ArchiveNumbering = ArchiveNumberingMode.Rolling, + ArchiveFileName = Path.Combine("${currentdir:cached=true}", SharedInfo.ArchivalLogsDirectory, SharedInfo.LogFile), ArchiveOldFileOnStartup = true, - CleanupFileName = false, - DeleteOldFileOnStartup = true, - FileName = Path.Combine("${currentdir}", SharedInfo.LogFile), + ArchiveSuffixFormat = ".{1:yyyyMMdd-HHmmss}", + FileName = Path.Combine("${currentdir:cached=true}", SharedInfo.LogFile), // Windows OS prevents other apps from reading file when actively holding exclusive (write) lock over it // We require read access for GET /Api/NLog/File ASF API usage, therefore we shouldn't keep the lock all the time @@ -265,6 +269,7 @@ internal static class Logging { // This is a temporary, bare, file-less configuration that must work until we're able to initialize it properly ConfigurationItemFactory.Default.ParseMessageTemplates = false; + LoggingConfiguration config = new(); #pragma warning disable CA2000 // False positive, we're adding this disposable object to the global scope, so we can't dispose it diff --git a/ArchiSteamFarm/NLog/Targets/HistoryTarget.cs b/ArchiSteamFarm/NLog/Targets/HistoryTarget.cs index 45fa40ad8..2644ccd7d 100644 --- a/ArchiSteamFarm/NLog/Targets/HistoryTarget.cs +++ b/ArchiSteamFarm/NLog/Targets/HistoryTarget.cs @@ -31,10 +31,8 @@ using NLog.Targets; namespace ArchiSteamFarm.NLog.Targets; -[Target(TargetName)] +[Target("History")] internal sealed class HistoryTarget : TargetWithLayout { - internal const string TargetName = "History"; - private const byte DefaultMaxCount = 20; internal IEnumerable ArchivedMessages => HistoryQueue; diff --git a/ArchiSteamFarm/NLog/Targets/SteamTarget.cs b/ArchiSteamFarm/NLog/Targets/SteamTarget.cs index d9125f1ab..086f46c11 100644 --- a/ArchiSteamFarm/NLog/Targets/SteamTarget.cs +++ b/ArchiSteamFarm/NLog/Targets/SteamTarget.cs @@ -30,17 +30,15 @@ using ArchiSteamFarm.Localization; using ArchiSteamFarm.Steam; using JetBrains.Annotations; using NLog; -using NLog.Config; using NLog.Layouts; using NLog.Targets; +using SteamKit2; namespace ArchiSteamFarm.NLog.Targets; [SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")] -[Target(TargetName)] +[Target("Steam")] internal sealed class SteamTarget : AsyncTaskTarget { - internal const string TargetName = "Steam"; - // This is NLog config property, it must have public get() and set() capabilities [UsedImplicitly] public Layout? BotName { get; set; } @@ -50,7 +48,6 @@ internal sealed class SteamTarget : AsyncTaskTarget { public ulong ChatGroupID { get; set; } // This is NLog config property, it must have public get() and set() capabilities - [RequiredParameter] [UsedImplicitly] public ulong SteamID { get; set; } @@ -59,12 +56,20 @@ internal sealed class SteamTarget : AsyncTaskTarget { // Keeping date in default layout also doesn't make much sense (Steam offers that), so we remove it by default public SteamTarget() => Layout = "${level:uppercase=true}|${logger}|${message}"; + protected override void InitializeTarget() { + base.InitializeTarget(); + + if ((SteamID == 0) || ((ChatGroupID == 0) && !new SteamID(SteamID).IsIndividualAccount)) { + throw new NLogConfigurationException(Strings.FormatErrorIsInvalid(nameof(SteamID))); + } + } + protected override async Task WriteAsyncTask(LogEventInfo logEvent, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(logEvent); Write(logEvent); - if ((SteamID == 0) || (Bot.Bots == null) || Bot.Bots.IsEmpty) { + if ((Bot.Bots == null) || Bot.Bots.IsEmpty) { return; } diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index cf630d4d8..fa4409a20 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -188,8 +188,8 @@ internal static class Program { // Add support for custom logging targets LogManager.Setup().SetupExtensions(static extensions => { - extensions.RegisterTarget(HistoryTarget.TargetName); - extensions.RegisterTarget(SteamTarget.TargetName); + extensions.RegisterTarget(); + extensions.RegisterTarget(); } ); diff --git a/ArchiSteamFarm/SharedInfo.cs b/ArchiSteamFarm/SharedInfo.cs index 5e50a560a..8e97065ce 100644 --- a/ArchiSteamFarm/SharedInfo.cs +++ b/ArchiSteamFarm/SharedInfo.cs @@ -35,7 +35,6 @@ public static class SharedInfo { [PublicAPI] public const string ConfigDirectory = "config"; - internal const string ArchivalLogFile = "log.{#}.txt"; internal const string ArchivalLogsDirectory = "logs"; internal const string ASF = nameof(ASF); internal const ulong ASFGroupSteamID = 103582791440160998; diff --git a/Directory.Packages.props b/Directory.Packages.props index f5e7d904f..e6e0a9f47 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -10,7 +10,7 @@ - +