Skip to content

Commit

Permalink
Merge tag '7.0.4' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
GeertvanHorrik committed Dec 20, 2024
2 parents 340e994 + 5f02e5e commit 235c95b
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/Orchestra.Core/Helpers/LogHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ public static ILogListener CreateFileLogListener(string? prefix = null)
Directory.CreateDirectory(directory);
}

var appDataService = ServiceLocator.Default.ResolveRequiredType<IAppDataService>();

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;
}
Expand Down
29 changes: 27 additions & 2 deletions src/Orchestra.Core/Logging/FileLogListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
using System;
using System.Reflection;
using Catel;
using Catel.IO;
using Catel.IoC;
using Catel.Logging;
using Catel.Services;

/// <summary>
/// Special purpose file logger that also writes Catel argument logging.
Expand All @@ -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)
Expand All @@ -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;
}
}
}
2 changes: 2 additions & 0 deletions src/Orchestra.Core/Orchestra.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
<ItemGroup>
<PackageReference Include="Catel.Fody" Version="4.9.0" PrivateAssets="all" />
<PackageReference Include="Fody" Version="6.9.1" PrivateAssets="all">
<PackageReference Include="Catel.MVVM" Version="6.0.5" />
<PackageReference Include="Fody" Version="6.8.2" PrivateAssets="all">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="LoadAssembliesOnStartup.Fody" Version="4.6.0" PrivateAssets="all" />
Expand Down
54 changes: 54 additions & 0 deletions src/Orchestra.Tests/Logging/FileLogListenerFacts.cs
Original file line number Diff line number Diff line change
@@ -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<IAppDataService>();
appDataServiceMock.Setup(x => x.GetApplicationDataDirectory(It.IsAny<ApplicationDataTarget>()))
.Returns<ApplicationDataTarget>(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<ApplicationDataTarget>()));
}
}

private class TestFileLogListener : FileLogListener
{
public TestFileLogListener(IAppDataService appDataService, Assembly? assembly = null)
: base(appDataService, assembly)
{
}

public bool HasCreatedDirectory { get { return Calls.Any(); } }

public List<string> Calls { get; private set; } = new List<string>();

protected override void CreateDirectory(string directory)
{
Calls.Add(directory);
}
}
}
}
2 changes: 2 additions & 0 deletions src/Orchestra.Tests/Orchestra.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="ModuleInit.Fody" Version="2.1.1" PrivateAssets="all" />
<PackageReference Include="NUnit" Version="4.3.0" PrivateAssets="all" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="NUnit" Version="4.2.2" PrivateAssets="all" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 235c95b

Please sign in to comment.