diff --git a/Build/set-platform-specific-config.ps1 b/Build/set-platform-specific-config.ps1 index d737891..64eb438 100644 --- a/Build/set-platform-specific-config.ps1 +++ b/Build/set-platform-specific-config.ps1 @@ -5,6 +5,8 @@ Param( [String] $operatingSystem ) +$ErrorActionPreference = "Stop" + if ($operatingSystem -eq 'Windows_NT') { $platformSpecificLogPath = 'C:/ProgramData/Elzik/fmsync' @@ -14,7 +16,11 @@ else $platformSpecificLogPath = '~/Library/Logs/Elzik/fmsync' } +Write-Output "Platform specific log path: $platformSpecificLogPath" + $appSettingsPath = "$outputDirectory/appSettings.json" +Write-Output "appSettings Path: $appSettingsPath" + (Get-Content $appSettingsPath).Replace('[PLATFORM_SPECIFIC_LOG_PATH]', $platformSpecificLogPath) ` | Set-Content $appSettingsPath \ No newline at end of file diff --git a/src/Elzik.FmSync.Application/Elzik.FmSync.Application.csproj b/src/Elzik.FmSync.Application/Elzik.FmSync.Application.csproj index 7ed464c..e16ceb9 100644 --- a/src/Elzik.FmSync.Application/Elzik.FmSync.Application.csproj +++ b/src/Elzik.FmSync.Application/Elzik.FmSync.Application.csproj @@ -18,7 +18,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/Elzik.FmSync.Application/FrontMatterFolderSynchroniser.cs b/src/Elzik.FmSync.Application/FrontMatterFolderSynchroniser.cs index c2bea87..976b22d 100644 --- a/src/Elzik.FmSync.Application/FrontMatterFolderSynchroniser.cs +++ b/src/Elzik.FmSync.Application/FrontMatterFolderSynchroniser.cs @@ -7,14 +7,14 @@ namespace Elzik.FmSync.Application; public class FrontMatterFolderSynchroniser( - ILogger logger, - IDirectory directory, - IFrontMatterFileSynchroniser frontMatterFileSynchroniser, + ILogger logger, + IDirectory directory, + IFrontMatterFileSynchroniser frontMatterFileSynchroniser, IOptions options) : IFrontMatterFolderSynchroniser { - private readonly ILogger _logger = logger + private readonly ILogger _logger = logger ?? throw new ArgumentNullException(nameof(logger)); - private readonly IDirectory _directory = directory + private readonly IDirectory _directory = directory ?? throw new ArgumentNullException(nameof(directory)); private readonly IFrontMatterFileSynchroniser _frontMatterFileSynchroniser = frontMatterFileSynchroniser ?? throw new ArgumentNullException(nameof(frontMatterFileSynchroniser)); @@ -24,7 +24,7 @@ public void SyncCreationDates(string directoryPath) { var loggingInfo = (StartTime: Stopwatch.GetTimestamp(), EditedCount: 0, ErrorCount: 0, TotalCount: 0); - _logger.LogDebug("Synchronising {FilenamePattern} files in {DirectoryPath}", + _logger.LogDebug("Synchronising {FilenamePattern} files in {DirectoryPath}", _options.FilenamePattern, directoryPath); var markdownFiles = _directory.EnumerateFiles(directoryPath, _options.FilenamePattern ?? string.Empty, @@ -48,13 +48,8 @@ public void SyncCreationDates(string directoryPath) catch (Exception e) { loggingInfo.ErrorCount++; - var additionalMessage = string.Empty; - if (e.InnerException != null) - { - additionalMessage = $" {e.InnerException.Message}"; - } - _logger.LogError("{MarkdownFilePath} - {ExceptionMessage}{AdditionalMessage}", - markDownFilePath, e.Message, additionalMessage); + _logger.LogError(e, "An error occurred whilst synchronising the creation date for {MarkdownFilePath}", + markDownFilePath); } } @@ -65,10 +60,10 @@ public void SyncCreationDates(string directoryPath) } _logger.LogInformation("Synchronised {EditedFileCount}{ErrorsMessage} files out " + - "of a total {TotalFileCount} in {TimeTaken}.", - loggingInfo.EditedCount, - errorsMessage, - loggingInfo.TotalCount, + "of a total {TotalFileCount} in {TimeTaken}.", + loggingInfo.EditedCount, + errorsMessage, + loggingInfo.TotalCount, Stopwatch.GetElapsedTime(loggingInfo.StartTime)); } } \ No newline at end of file diff --git a/src/Elzik.FmSync.Console/Elzik.FmSync.Console.csproj b/src/Elzik.FmSync.Console/Elzik.FmSync.Console.csproj index 67ac0b7..5e7b4f2 100644 --- a/src/Elzik.FmSync.Console/Elzik.FmSync.Console.csproj +++ b/src/Elzik.FmSync.Console/Elzik.FmSync.Console.csproj @@ -24,7 +24,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -41,11 +41,11 @@ - + - + diff --git a/src/Elzik.FmSync.Infrastructure/Elzik.FmSync.Infrastructure.csproj b/src/Elzik.FmSync.Infrastructure/Elzik.FmSync.Infrastructure.csproj index 7d7258e..e6c895a 100644 --- a/src/Elzik.FmSync.Infrastructure/Elzik.FmSync.Infrastructure.csproj +++ b/src/Elzik.FmSync.Infrastructure/Elzik.FmSync.Infrastructure.csproj @@ -16,7 +16,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/Elzik.FmSync.Worker/Elzik.FmSync.Worker.csproj b/src/Elzik.FmSync.Worker/Elzik.FmSync.Worker.csproj index 794b461..066a375 100644 --- a/src/Elzik.FmSync.Worker/Elzik.FmSync.Worker.csproj +++ b/src/Elzik.FmSync.Worker/Elzik.FmSync.Worker.csproj @@ -17,13 +17,13 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - + - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -42,11 +42,11 @@ - + - + diff --git a/src/Elzik.FmSync.Worker/FmSyncWorker.cs b/src/Elzik.FmSync.Worker/FmSyncWorker.cs index 866b258..3ed8ff9 100644 --- a/src/Elzik.FmSync.Worker/FmSyncWorker.cs +++ b/src/Elzik.FmSync.Worker/FmSyncWorker.cs @@ -27,23 +27,6 @@ public FmSyncWorker(ILogger logger, IOptions watch } protected override async Task ExecuteAsync(CancellationToken stoppingToken) - { - try - { - StartWorker(); - } - catch (Exception ex) - { - _logger.LogCritical(ex, "A problem occurred whilst starting the worker. " + - "{ExceptionMessage}", ex.Message); - - throw; - } - - await Task.Yield(); - } - - private void StartWorker() { _logger.LogInformation("fmsync {Version} has started.", GetProductVersion()); _logger.LogDebug("File synchroniation is implemented by {SyncName}", _fileSynchroniser.GetType().Name); @@ -79,6 +62,8 @@ private void StartWorker() "the {ConfigSection}:{ConfigItem} configuration.", nameof(WatcherOptions), nameof(WatcherOptions.WatchedDirectoryPaths)); } + + await Task.Yield(); } private static string GetProductVersion() diff --git a/src/Elzik.FmSync.Worker/Program.cs b/src/Elzik.FmSync.Worker/Program.cs index c0292cf..fbdd4e2 100644 --- a/src/Elzik.FmSync.Worker/Program.cs +++ b/src/Elzik.FmSync.Worker/Program.cs @@ -7,10 +7,12 @@ using Polly; using Elzik.FmSync.Application; +var appSettingsPath = Path.Join(AppContext.BaseDirectory, "appSettings.json"); + var host = Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration((_, config) => { - config.AddJsonFile("appSettings.json", false); + config.AddJsonFile(appSettingsPath, false); }) .UseSerilog((context, config) => config.ReadFrom.Configuration(context.Configuration)) .ConfigureServices((hostContext, services) => diff --git a/tests/Elzik.FmSync.Application.Tests.Unit/Elzik.FmSync.Application.Tests.Unit.csproj b/tests/Elzik.FmSync.Application.Tests.Unit/Elzik.FmSync.Application.Tests.Unit.csproj index 59e7a98..89f5bf3 100644 --- a/tests/Elzik.FmSync.Application.Tests.Unit/Elzik.FmSync.Application.Tests.Unit.csproj +++ b/tests/Elzik.FmSync.Application.Tests.Unit/Elzik.FmSync.Application.Tests.Unit.csproj @@ -18,7 +18,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -33,8 +33,8 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/tests/Elzik.FmSync.Application.Tests.Unit/FrontMatterFolderSynchroniserTests.cs b/tests/Elzik.FmSync.Application.Tests.Unit/FrontMatterFolderSynchroniserTests.cs index d684e30..184fb3d 100644 --- a/tests/Elzik.FmSync.Application.Tests.Unit/FrontMatterFolderSynchroniserTests.cs +++ b/tests/Elzik.FmSync.Application.Tests.Unit/FrontMatterFolderSynchroniserTests.cs @@ -44,7 +44,7 @@ public void SyncCreationDates_DirectoryPathSupplied_OnlyLogs() { // Arrange var testDirectoryPath = _fixture.Create(); - + // Act _frontMatterFolderSynchroniser.SyncCreationDates(testDirectoryPath); @@ -105,7 +105,7 @@ public void SyncCreationDates_WithMarkDownFiles_LogsSummary() } [Fact] - public void SyncCreationDates_SyncFailsWithInnerException_LogsError() + public void SyncCreationDates_SyncFails_LogsException() { // Arrange var testDirectoryPath = _fixture.Create(); @@ -119,27 +119,8 @@ public void SyncCreationDates_SyncFailsWithInnerException_LogsError() _frontMatterFolderSynchroniser.SyncCreationDates(testDirectoryPath); // Assert - _mockLogger.Received(1).Log( LogLevel.Error, - testFile.Key + " - " + testException.Message + " " + testException.InnerException?.Message); - } - - [Fact] - public void SyncCreationDates_SyncFailsWithoutInnerException_LogsError() - { - // Arrange - var testDirectoryPath = _fixture.Create(); - var testFile = _fixture.Create>(); - var testFiles = new List> { testFile }; - SetMockDirectoryFilePaths(testDirectoryPath, testFiles); - var testException = new Exception(_fixture.Create()); - _mockFileSynchroniser.SyncCreationDate(testFile.Key).Throws(testException); - - // Act - _frontMatterFolderSynchroniser.SyncCreationDates(testDirectoryPath); - - // Assert - _mockLogger.Received(1).Log(LogLevel.Error, - testFile.Key + " - " + testException.Message); + _mockLogger.Received(1).Log(LogLevel.Error, "An error occurred whilst synchronising the creation date for " + + testFile.Key); } [Fact] @@ -148,7 +129,7 @@ public void SyncCreationDates_SyncFails_LogsSummary() // Arrange var testDirectoryPath = _fixture.Create(); var testFailingFile = new KeyValuePair(_fixture.Create(), false); - var testFiles = new [] + var testFiles = new[] { testFailingFile, new (_fixture.Create(), false), diff --git a/tests/Elzik.FmSync.Console.Tests.Functional/Elzik.FmSync.Console.Tests.Functional.csproj b/tests/Elzik.FmSync.Console.Tests.Functional/Elzik.FmSync.Console.Tests.Functional.csproj index 835daec..258d684 100644 --- a/tests/Elzik.FmSync.Console.Tests.Functional/Elzik.FmSync.Console.Tests.Functional.csproj +++ b/tests/Elzik.FmSync.Console.Tests.Functional/Elzik.FmSync.Console.Tests.Functional.csproj @@ -28,7 +28,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -38,7 +38,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -56,7 +56,7 @@ - + diff --git a/tests/Elzik.FmSync.Infrastructure.Tests.Integration/Elzik.FmSync.Infrastructure.Tests.Integration.csproj b/tests/Elzik.FmSync.Infrastructure.Tests.Integration/Elzik.FmSync.Infrastructure.Tests.Integration.csproj index 022d176..e75012d 100644 --- a/tests/Elzik.FmSync.Infrastructure.Tests.Integration/Elzik.FmSync.Infrastructure.Tests.Integration.csproj +++ b/tests/Elzik.FmSync.Infrastructure.Tests.Integration/Elzik.FmSync.Infrastructure.Tests.Integration.csproj @@ -25,7 +25,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -35,7 +35,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/tests/Elzik.FmSync.Worker.Tests.Functional/Elzik.FmSync.Worker.Tests.Functional.csproj b/tests/Elzik.FmSync.Worker.Tests.Functional/Elzik.FmSync.Worker.Tests.Functional.csproj index 92cb446..46f89ab 100644 --- a/tests/Elzik.FmSync.Worker.Tests.Functional/Elzik.FmSync.Worker.Tests.Functional.csproj +++ b/tests/Elzik.FmSync.Worker.Tests.Functional/Elzik.FmSync.Worker.Tests.Functional.csproj @@ -28,7 +28,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -38,7 +38,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -56,7 +56,7 @@ - +