diff --git a/BrowseRouter/Config/ConfigService.cs b/BrowseRouter/Config/ConfigService.cs index 8c76fb0..a75dc5d 100644 --- a/BrowseRouter/Config/ConfigService.cs +++ b/BrowseRouter/Config/ConfigService.cs @@ -1,4 +1,5 @@ using System.Text.Json; +using BrowseRouter.Infrastructure; using BrowseRouter.Model; namespace BrowseRouter.Config; @@ -14,9 +15,15 @@ internal class ConfigService(Config config) : IConfigService public LogPreference GetLogPreference() => new() { IsEnabled = config.Log.Enabled, - File = string.IsNullOrWhiteSpace(config.Log.File) - ? LogPreference.DefaultLogFile - : config.Log.File! + File = string.IsNullOrWhiteSpace(config.Log.File) switch + { + true => LogPreference.DefaultLogFile, + false => Path.IsPathFullyQualified(config.Log.File) switch + { + true => config.Log.File, + false => Path.Combine(App.ExePath, config.Log.File) + } + } }; public IEnumerable GetUrlPreferences(ConfigType configType) @@ -53,7 +60,14 @@ public async Task> GetFiltersAsync() if (string.IsNullOrWhiteSpace(config.FiltersFile)) return []; - var json = await File.ReadAllTextAsync(config.FiltersFile); + var dir = Path.GetDirectoryName(App.ExePath)!; + string path = Path.IsPathFullyQualified(config.FiltersFile) switch + { + true => config.FiltersFile, + false => Path.Combine(dir, config.FiltersFile) + }; + + var json = await File.ReadAllTextAsync(path); return JsonSerializer.Deserialize(json, SourceGenerationContext.Default.FilterPreferenceList) ?? []; } } \ No newline at end of file diff --git a/BrowseRouter/Infrastructure/Actions.cs b/BrowseRouter/Infrastructure/Actions.cs index 79f30d4..a534c31 100644 --- a/BrowseRouter/Infrastructure/Actions.cs +++ b/BrowseRouter/Infrastructure/Actions.cs @@ -2,7 +2,7 @@ public static class Actions { - public static bool TryRun(Action a) + public static bool TryRun(Action a, bool logOnlyToConsole = false) { try { @@ -11,7 +11,15 @@ public static bool TryRun(Action a) } catch (Exception e) { - Log.Write($"{e}"); + if (logOnlyToConsole) + { + Console.WriteLine(e); + } + else + { + Log.Write($"{e}"); + } + return false; } } diff --git a/BrowseRouter/Infrastructure/App.cs b/BrowseRouter/Infrastructure/App.cs index 47bc7bd..2c838e3 100644 --- a/BrowseRouter/Infrastructure/App.cs +++ b/BrowseRouter/Infrastructure/App.cs @@ -1,6 +1,4 @@ -using System.Reflection; - -namespace BrowseRouter.Infrastructure; +namespace BrowseRouter.Infrastructure; public static class App { diff --git a/BrowseRouter/Infrastructure/Log.cs b/BrowseRouter/Infrastructure/Log.cs index daa2d2c..8714bba 100644 --- a/BrowseRouter/Infrastructure/Log.cs +++ b/BrowseRouter/Infrastructure/Log.cs @@ -22,7 +22,7 @@ private static void EnsureLogDirExists() string? parent = Path.GetDirectoryName(Preference.File); if (parent is not null) { - Directory.CreateDirectory(parent); + Actions.TryRun(() => Directory.CreateDirectory(parent), logOnlyToConsole: true); } } @@ -36,9 +36,11 @@ private static void TryWrite(string message) writer.WriteLine(message); return; } - catch (Exception) + catch (Exception e) { + Console.WriteLine($"Failed to write log file {Preference.File} on attempt {i + 1}: {e}"); } + Thread.Sleep(100); } } } diff --git a/BrowseRouter/config.json b/BrowseRouter/config.json index 0d2aba9..528a4de 100644 --- a/BrowseRouter/config.json +++ b/BrowseRouter/config.json @@ -4,8 +4,7 @@ "silent": false }, "log": { - "enabled": true, - "file": "logs/app.log" + "enabled": true }, "urls": { "*.google.com": "chrome", diff --git a/Directory.Build.props b/Directory.Build.props index 65c40de..4237f83 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,12 +1,11 @@ 4 - true enable enable latest - 0.15.0 + 0.15.1 BrowseRouter EnduraByte LLC 2025