From d8d3b0ca2d876230ab10a0b4cd91a3e60242ea16 Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Fri, 20 Dec 2024 11:46:02 +0100 Subject: [PATCH 1/4] Allow override of FileLogListener.GetApplicationDataDirectory based on IAppDataService --- src/Orchestra.Core/Helpers/LogHelper.cs | 4 +- src/Orchestra.Core/Logging/FileLogListener.cs | 29 +++++++++- src/Orchestra.Core/Orchestra.Core.csproj | 1 + .../Logging/FileLogListenerFacts.cs | 54 +++++++++++++++++++ src/Orchestra.Tests/Orchestra.Tests.csproj | 1 + 5 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 src/Orchestra.Tests/Logging/FileLogListenerFacts.cs diff --git a/src/Orchestra.Core/Helpers/LogHelper.cs b/src/Orchestra.Core/Helpers/LogHelper.cs index 2dcfb93f3..4285807fe 100644 --- a/src/Orchestra.Core/Helpers/LogHelper.cs +++ b/src/Orchestra.Core/Helpers/LogHelper.cs @@ -104,8 +104,10 @@ public static ILogListener CreateFileLogListener(string? prefix = null) Directory.CreateDirectory(directory); } + var appDataService = ServiceLocator.Default.ResolveRequiredType(); + var fileName = Path.Combine(directory, prefix + "_{Date}_{Time}_{ProcessId}"); - var fileLogListener = new Orchestra.Logging.FileLogListener(fileName, MaxFileLogSize); + var fileLogListener = new Orchestra.Logging.FileLogListener(appDataService, fileName, MaxFileLogSize); return fileLogListener; } diff --git a/src/Orchestra.Core/Logging/FileLogListener.cs b/src/Orchestra.Core/Logging/FileLogListener.cs index df5f0f14f..a5a545c05 100644 --- a/src/Orchestra.Core/Logging/FileLogListener.cs +++ b/src/Orchestra.Core/Logging/FileLogListener.cs @@ -3,7 +3,10 @@ using System; using System.Reflection; using Catel; + using Catel.IO; + using Catel.IoC; using Catel.Logging; + using Catel.Services; /// /// Special purpose file logger that also writes Catel argument logging. @@ -15,14 +18,18 @@ public class FileLogListener : Catel.Logging.FileLogListener { private static readonly Type ArgumentType = typeof(Argument); - public FileLogListener(Assembly? assembly = null) + private readonly IAppDataService _appDataService; + + public FileLogListener(IAppDataService appDataService, Assembly? assembly = null) : base(assembly) { + _appDataService = appDataService; } - public FileLogListener(string filePath, int maxSizeInKiloBytes, Assembly? assembly = null) + public FileLogListener(IAppDataService appDataService, string filePath, int maxSizeInKiloBytes, Assembly? assembly = null) : base(filePath, maxSizeInKiloBytes, assembly) { + _appDataService = appDataService; } protected override bool ShouldIgnoreLogMessage(ILog log, string message, LogEvent logEvent, object? extraData, LogData? logData, DateTime time) @@ -35,5 +42,23 @@ protected override bool ShouldIgnoreLogMessage(ILog log, string message, LogEven return base.ShouldIgnoreLogMessage(log, message, logEvent, extraData, logData, time); } + + protected override string GetApplicationDataDirectory(ApplicationDataTarget target, string company, string product) + { + var baseDirectory = _appDataService.GetApplicationDataDirectory(target); + var directory = baseDirectory; + + if (!string.IsNullOrWhiteSpace(company)) + { + directory = System.IO.Path.Combine(directory, company); + } + + if (!string.IsNullOrWhiteSpace(product)) + { + directory = System.IO.Path.Combine(directory, product); + } + + return directory; + } } } diff --git a/src/Orchestra.Core/Orchestra.Core.csproj b/src/Orchestra.Core/Orchestra.Core.csproj index 1f1a3b873..75024bd60 100644 --- a/src/Orchestra.Core/Orchestra.Core.csproj +++ b/src/Orchestra.Core/Orchestra.Core.csproj @@ -24,6 +24,7 @@ + runtime; build; native; contentfiles; analyzers diff --git a/src/Orchestra.Tests/Logging/FileLogListenerFacts.cs b/src/Orchestra.Tests/Logging/FileLogListenerFacts.cs new file mode 100644 index 000000000..f0845d286 --- /dev/null +++ b/src/Orchestra.Tests/Logging/FileLogListenerFacts.cs @@ -0,0 +1,54 @@ +namespace Orchestra.Tests.Logging +{ + using System.Collections.Generic; + using System.Linq; + using System.Reflection; + using System.Threading.Tasks; + using Catel.IO; + using Catel.Services; + using Moq; + using NUnit.Framework; + using Orchestra.Logging; + + public class FileLogListenerFacts + { + public class The_GetApplicationDataDirectory_Method + { + [TestCase("", "")] + [TestCase("company", "")] + [TestCase("", "product")] + [TestCase("company", "product")] + public async Task Returns_Correct_Version_Async(string company, string product) + { + var appDataServiceMock = new Mock(); + appDataServiceMock.Setup(x => x.GetApplicationDataDirectory(It.IsAny())) + .Returns(x => "%appdata%"); + + var fileLogListener = new TestFileLogListener(appDataServiceMock.Object, null); + + Assert.That(fileLogListener.FilePath, Is.Not.Empty); + + Assert.That(fileLogListener.Calls.Count, Is.Not.EqualTo(0)); + + appDataServiceMock.Verify(x => x.GetApplicationDataDirectory(It.IsAny())); + } + } + + private class TestFileLogListener : FileLogListener + { + public TestFileLogListener(IAppDataService appDataService, Assembly? assembly = null) + : base(appDataService, assembly) + { + } + + public bool HasCreatedDirectory { get { return Calls.Any(); } } + + public List Calls { get; private set; } = new List(); + + protected override void CreateDirectory(string directory) + { + Calls.Add(directory); + } + } + } +} diff --git a/src/Orchestra.Tests/Orchestra.Tests.csproj b/src/Orchestra.Tests/Orchestra.Tests.csproj index 2f35130d5..879473308 100644 --- a/src/Orchestra.Tests/Orchestra.Tests.csproj +++ b/src/Orchestra.Tests/Orchestra.Tests.csproj @@ -27,6 +27,7 @@ + all From 778d4a4671017e2eb8b1afc6f12a1b9b2fdad6a0 Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Fri, 20 Dec 2024 11:47:11 +0100 Subject: [PATCH 2/4] Approve API --- ...ts.Orchestra_Core_HasNoBreakingChanges_Async.verified.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Orchestra.Tests/PublicApiFacts.Orchestra_Core_HasNoBreakingChanges_Async.verified.txt b/src/Orchestra.Tests/PublicApiFacts.Orchestra_Core_HasNoBreakingChanges_Async.verified.txt index 86726a023..e1101f781 100644 --- a/src/Orchestra.Tests/PublicApiFacts.Orchestra_Core_HasNoBreakingChanges_Async.verified.txt +++ b/src/Orchestra.Tests/PublicApiFacts.Orchestra_Core_HasNoBreakingChanges_Async.verified.txt @@ -538,8 +538,9 @@ namespace Orchestra.Logging { public class FileLogListener : Catel.Logging.FileLogListener { - public FileLogListener(System.Reflection.Assembly? assembly = null) { } - public FileLogListener(string filePath, int maxSizeInKiloBytes, System.Reflection.Assembly? assembly = null) { } + public FileLogListener(Catel.Services.IAppDataService appDataService, System.Reflection.Assembly? assembly = null) { } + public FileLogListener(Catel.Services.IAppDataService appDataService, string filePath, int maxSizeInKiloBytes, System.Reflection.Assembly? assembly = null) { } + protected override string GetApplicationDataDirectory(Catel.IO.ApplicationDataTarget target, string company, string product) { } protected override bool ShouldIgnoreLogMessage(Catel.Logging.ILog log, string message, Catel.Logging.LogEvent logEvent, object? extraData, Catel.Logging.LogData? logData, System.DateTime time) { } } public class RichTextBoxLogListener : Catel.Logging.LogListenerBase From a3aa24e16c676bd31482073b54a64eb0100db740 Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Fri, 20 Dec 2024 13:51:23 +0100 Subject: [PATCH 3/4] Bump Catel --- src/Orchestra.Core/Orchestra.Core.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Orchestra.Core/Orchestra.Core.csproj b/src/Orchestra.Core/Orchestra.Core.csproj index 75024bd60..442d23ca9 100644 --- a/src/Orchestra.Core/Orchestra.Core.csproj +++ b/src/Orchestra.Core/Orchestra.Core.csproj @@ -24,7 +24,7 @@ - + runtime; build; native; contentfiles; analyzers From faaa7a272790e662ea3e9db69a3a79ecc51a5331 Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Fri, 20 Dec 2024 14:14:13 +0100 Subject: [PATCH 4/4] Bump Catel --- src/Orchestra.Core/Orchestra.Core.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Orchestra.Core/Orchestra.Core.csproj b/src/Orchestra.Core/Orchestra.Core.csproj index 442d23ca9..8299d6fc9 100644 --- a/src/Orchestra.Core/Orchestra.Core.csproj +++ b/src/Orchestra.Core/Orchestra.Core.csproj @@ -24,7 +24,7 @@ - + runtime; build; native; contentfiles; analyzers