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 84c143278..df640496c 100644 --- a/src/Orchestra.Core/Orchestra.Core.csproj +++ b/src/Orchestra.Core/Orchestra.Core.csproj @@ -26,6 +26,8 @@ + + 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 0c9c21caa..d9ea8ae17 100644 --- a/src/Orchestra.Tests/Orchestra.Tests.csproj +++ b/src/Orchestra.Tests/Orchestra.Tests.csproj @@ -28,6 +28,8 @@ + + all runtime; build; native; contentfiles; analyzers; buildtransitive 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 b6fd4c943..f68f2a01e 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