diff --git a/Flow.Launcher.Core/Resource/Internationalization.cs b/Flow.Launcher.Core/Resource/Internationalization.cs index 1dfc6d4ed..b32b09e8f 100644 --- a/Flow.Launcher.Core/Resource/Internationalization.cs +++ b/Flow.Launcher.Core/Resource/Internationalization.cs @@ -18,12 +18,15 @@ namespace Flow.Launcher.Core.Resource { private static readonly string ClassName = nameof(Internationalization); + // We should not initialize API in static constructor because it will create another API instance + private static IPublicAPI api = null; + private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService(); + private const string Folder = "Languages"; private const string DefaultLanguageCode = "en"; private const string DefaultFile = "en.xaml"; private const string Extension = ".xaml"; private readonly Settings _settings; - private readonly IPublicAPI _api; private readonly List _languageDirectories = new(); private readonly List _oldResources = new(); private readonly string SystemLanguageCode; @@ -31,7 +34,6 @@ namespace Flow.Launcher.Core.Resource public Internationalization(Settings settings) { _settings = settings; - _api = Ioc.Default.GetRequiredService(); AddFlowLauncherLanguageDirectory(); SystemLanguageCode = GetSystemLanguageCodeAtStartup(); } @@ -83,7 +85,7 @@ namespace Flow.Launcher.Core.Resource } else { - _api.LogError(ClassName, $"Can't find plugin path <{location}> for <{plugin.Metadata.Name}>"); + API.LogError(ClassName, $"Can't find plugin path <{location}> for <{plugin.Metadata.Name}>"); } } @@ -147,13 +149,13 @@ namespace Flow.Launcher.Core.Resource _settings.Language = isSystem ? Constant.SystemLanguageCode : language.LanguageCode; } - private Language GetLanguageByLanguageCode(string languageCode) + private static Language GetLanguageByLanguageCode(string languageCode) { var lowercase = languageCode.ToLower(); var language = AvailableLanguages.GetAvailableLanguages().FirstOrDefault(o => o.LanguageCode.ToLower() == lowercase); if (language == null) { - _api.LogError(ClassName, $"Language code can't be found <{languageCode}>"); + API.LogError(ClassName, $"Language code can't be found <{languageCode}>"); return AvailableLanguages.English; } else @@ -242,7 +244,7 @@ namespace Flow.Launcher.Core.Resource return list; } - public string GetTranslation(string key) + public static string GetTranslation(string key) { var translation = Application.Current.TryFindResource(key); if (translation is string) @@ -251,7 +253,7 @@ namespace Flow.Launcher.Core.Resource } else { - _api.LogError(ClassName, $"No Translation for key {key}"); + API.LogError(ClassName, $"No Translation for key {key}"); return $"No Translation for key {key}"; } } @@ -269,12 +271,12 @@ namespace Flow.Launcher.Core.Resource } catch (Exception e) { - _api.LogException(ClassName, $"Failed for <{p.Metadata.Name}>", e); + API.LogException(ClassName, $"Failed for <{p.Metadata.Name}>", e); } } } - private string LanguageFile(string folder, string language) + private static string LanguageFile(string folder, string language) { if (Directory.Exists(folder)) { @@ -285,7 +287,7 @@ namespace Flow.Launcher.Core.Resource } else { - _api.LogError(ClassName, $"Language path can't be found <{path}>"); + API.LogError(ClassName, $"Language path can't be found <{path}>"); var english = Path.Combine(folder, DefaultFile); if (File.Exists(english)) { @@ -293,7 +295,7 @@ namespace Flow.Launcher.Core.Resource } else { - _api.LogError(ClassName, $"Default English Language path can't be found <{path}>"); + API.LogError(ClassName, $"Default English Language path can't be found <{path}>"); return string.Empty; } } diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index a9cd1a8b9..87677f58a 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -148,8 +148,8 @@ namespace Flow.Launcher Ioc.Default.GetRequiredService().PreStartCleanUpAfterPortabilityUpdate(); - Log.Info("|App.OnStartup|Begin Flow Launcher startup ----------------------------------------------------"); - Log.Info($"|App.OnStartup|Runtime info:{ErrorReporting.RuntimeInfo()}"); + API.LogInfo(ClassName, "Begin Flow Launcher startup ----------------------------------------------------"); + API.LogInfo(ClassName, "Runtime info:{ErrorReporting.RuntimeInfo()}"); RegisterAppDomainExceptions(); RegisterDispatcherUnhandledException(); @@ -176,7 +176,7 @@ namespace Flow.Launcher _mainWindow = new MainWindow(); - Log.Info($"|App.OnStartup|Dependencies Info:{ErrorReporting.DependenciesInfo()}"); + API.LogInfo(ClassName, "Dependencies Info:{ErrorReporting.DependenciesInfo()}"); Current.MainWindow = _mainWindow; Current.MainWindow.Title = Constant.FlowLauncher; @@ -192,7 +192,7 @@ namespace Flow.Launcher AutoUpdates(); API.SaveAppAllSettings(); - Log.Info("|App.OnStartup|End Flow Launcher startup ----------------------------------------------------"); + API.LogInfo(ClassName, "End Flow Launcher startup ----------------------------------------------------"); }); } @@ -250,19 +250,19 @@ namespace Flow.Launcher { AppDomain.CurrentDomain.ProcessExit += (s, e) => { - Log.Info("|App.RegisterExitEvents|Process Exit"); + API.LogInfo(ClassName, "Process Exit"); Dispose(); }; Current.Exit += (s, e) => { - Log.Info("|App.RegisterExitEvents|Application Exit"); + API.LogInfo(ClassName, "Application Exit"); Dispose(); }; Current.SessionEnding += (s, e) => { - Log.Info("|App.RegisterExitEvents|Session Ending"); + API.LogInfo(ClassName, "Session Ending"); Dispose(); }; } @@ -326,7 +326,7 @@ namespace Flow.Launcher API.StopwatchLogInfo(ClassName, "Dispose cost", () => { - Log.Info("|App.Dispose|Begin Flow Launcher dispose ----------------------------------------------------"); + API.LogInfo(ClassName, "Begin Flow Launcher dispose ----------------------------------------------------"); if (disposing) { @@ -336,7 +336,7 @@ namespace Flow.Launcher _mainVM?.Dispose(); } - Log.Info("|App.Dispose|End Flow Launcher dispose ----------------------------------------------------"); + API.LogInfo(ClassName, "End Flow Launcher dispose ----------------------------------------------------"); }); } diff --git a/Flow.Launcher/Converters/QuerySuggestionBoxConverter.cs b/Flow.Launcher/Converters/QuerySuggestionBoxConverter.cs index 0d6f2e469..eb492e334 100644 --- a/Flow.Launcher/Converters/QuerySuggestionBoxConverter.cs +++ b/Flow.Launcher/Converters/QuerySuggestionBoxConverter.cs @@ -4,13 +4,14 @@ using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Media; -using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.ViewModel; namespace Flow.Launcher.Converters; public class QuerySuggestionBoxConverter : IMultiValueConverter { + private static readonly string ClassName = nameof(QuerySuggestionBoxConverter); + public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { // values[0] is TextBox: The textbox displaying the autocomplete suggestion @@ -64,7 +65,7 @@ public class QuerySuggestionBoxConverter : IMultiValueConverter } catch (Exception e) { - Log.Exception(nameof(QuerySuggestionBoxConverter), "fail to convert text for suggestion box", e); + App.API.LogException(ClassName, "fail to convert text for suggestion box", e); return string.Empty; } } diff --git a/Flow.Launcher/Helper/AutoStartup.cs b/Flow.Launcher/Helper/AutoStartup.cs index c5e20504b..568ea7944 100644 --- a/Flow.Launcher/Helper/AutoStartup.cs +++ b/Flow.Launcher/Helper/AutoStartup.cs @@ -11,6 +11,8 @@ namespace Flow.Launcher.Helper; public class AutoStartup { + private static readonly string ClassName = nameof(AutoStartup); + private const string StartupPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; private const string LogonTaskName = $"{Constant.FlowLauncher} Startup"; private const string LogonTaskDesc = $"{Constant.FlowLauncher} Auto Startup"; @@ -34,7 +36,7 @@ public class AutoStartup } catch (Exception e) { - Log.Error("AutoStartup", $"Ignoring non-critical registry error (querying if enabled): {e}"); + App.API.LogError(ClassName, $"Ignoring non-critical registry error (querying if enabled): {e}"); } return false; @@ -61,7 +63,7 @@ public class AutoStartup } catch (Exception e) { - Log.Error("AutoStartup", $"Failed to check logon task: {e}"); + App.API.LogError(ClassName, $"Failed to check logon task: {e}"); } } @@ -112,7 +114,7 @@ public class AutoStartup } catch (Exception e) { - Log.Error("AutoStartup", $"Failed to disable auto-startup: {e}"); + App.API.LogError(ClassName, $"Failed to disable auto-startup: {e}"); throw; } } @@ -133,7 +135,7 @@ public class AutoStartup } catch (Exception e) { - Log.Error("AutoStartup", $"Failed to enable auto-startup: {e}"); + App.API.LogError(ClassName, $"Failed to enable auto-startup: {e}"); throw; } } @@ -161,7 +163,7 @@ public class AutoStartup } catch (Exception e) { - Log.Error("AutoStartup", $"Failed to schedule logon task: {e}"); + App.API.LogError(ClassName, $"Failed to schedule logon task: {e}"); return false; } } @@ -176,7 +178,7 @@ public class AutoStartup } catch (Exception e) { - Log.Error("AutoStartup", $"Failed to unschedule logon task: {e}"); + App.API.LogError(ClassName, $"Failed to unschedule logon task: {e}"); return false; } } diff --git a/Flow.Launcher/Helper/HotKeyMapper.cs b/Flow.Launcher/Helper/HotKeyMapper.cs index 0771a6074..1e83415cc 100644 --- a/Flow.Launcher/Helper/HotKeyMapper.cs +++ b/Flow.Launcher/Helper/HotKeyMapper.cs @@ -12,6 +12,8 @@ namespace Flow.Launcher.Helper; internal static class HotKeyMapper { + private static readonly string ClassName = nameof(HotKeyMapper); + private static Settings _settings; private static MainViewModel _mainViewModel; @@ -51,7 +53,7 @@ internal static class HotKeyMapper } catch (Exception e) { - Log.Error( + App.API.LogError(ClassName, string.Format("|HotkeyMapper.SetWithChefKeys|Error registering hotkey: {0} \nStackTrace:{1}", e.Message, e.StackTrace)); @@ -76,7 +78,7 @@ internal static class HotKeyMapper } catch (Exception e) { - Log.Error( + App.API.LogError(ClassName, string.Format("|HotkeyMapper.SetHotkey|Error registering hotkey {2}: {0} \nStackTrace:{1}", e.Message, e.StackTrace, @@ -102,7 +104,7 @@ internal static class HotKeyMapper } catch (Exception e) { - Log.Error( + App.API.LogError(ClassName, string.Format("|HotkeyMapper.RemoveHotkey|Error removing hotkey: {0} \nStackTrace:{1}", e.Message, e.StackTrace)); diff --git a/Flow.Launcher/MessageBoxEx.xaml.cs b/Flow.Launcher/MessageBoxEx.xaml.cs index 3d94769d0..a55aeb811 100644 --- a/Flow.Launcher/MessageBoxEx.xaml.cs +++ b/Flow.Launcher/MessageBoxEx.xaml.cs @@ -4,13 +4,13 @@ using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using Flow.Launcher.Infrastructure; -using Flow.Launcher.Infrastructure.Image; -using Flow.Launcher.Infrastructure.Logger; namespace Flow.Launcher { public partial class MessageBoxEx : Window { + private static readonly string ClassName = nameof(MessageBoxEx); + private static MessageBoxEx msgBox; private static MessageBoxResult _result = MessageBoxResult.None; @@ -59,7 +59,7 @@ namespace Flow.Launcher } catch (Exception e) { - Log.Error($"|MessageBoxEx.Show|An error occurred: {e.Message}"); + App.API.LogError(ClassName, $"An error occurred: {e.Message}"); msgBox = null; return MessageBoxResult.None; } diff --git a/Flow.Launcher/Msg.xaml.cs b/Flow.Launcher/Msg.xaml.cs index ff9accd62..110235cb5 100644 --- a/Flow.Launcher/Msg.xaml.cs +++ b/Flow.Launcher/Msg.xaml.cs @@ -5,7 +5,6 @@ using System.Windows.Forms; using System.Windows.Input; using System.Windows.Media.Animation; using Flow.Launcher.Infrastructure; -using Flow.Launcher.Infrastructure.Image; namespace Flow.Launcher { diff --git a/Flow.Launcher/Notification.cs b/Flow.Launcher/Notification.cs index 30b3a0673..be81e6ec6 100644 --- a/Flow.Launcher/Notification.cs +++ b/Flow.Launcher/Notification.cs @@ -1,5 +1,4 @@ using Flow.Launcher.Infrastructure; -using Flow.Launcher.Infrastructure.Logger; using Microsoft.Toolkit.Uwp.Notifications; using System; using System.IO; @@ -9,6 +8,8 @@ namespace Flow.Launcher { internal static class Notification { + private static readonly string ClassName = nameof(Notification); + internal static bool legacy = !Win32Helper.IsNotificationSupported(); internal static void Uninstall() @@ -51,12 +52,12 @@ namespace Flow.Launcher { // Temporary fix for the Windows 11 notification issue // Possibly from 22621.1413 or 22621.1485, judging by post time of #2024 - Log.Exception("Flow.Launcher.Notification|Notification InvalidOperationException Error", e); + App.API.LogException(ClassName, "Notification InvalidOperationException Error", e); LegacyShow(title, subTitle, iconPath); } catch (Exception e) { - Log.Exception("Flow.Launcher.Notification|Notification Error", e); + App.API.LogException(ClassName, "Notification Error", e); LegacyShow(title, subTitle, iconPath); } } diff --git a/Flow.Launcher/ProgressBoxEx.xaml.cs b/Flow.Launcher/ProgressBoxEx.xaml.cs index 2395bdf34..840c8bade 100644 --- a/Flow.Launcher/ProgressBoxEx.xaml.cs +++ b/Flow.Launcher/ProgressBoxEx.xaml.cs @@ -2,12 +2,13 @@ using System.Threading.Tasks; using System.Windows; using System.Windows.Input; -using Flow.Launcher.Infrastructure.Logger; namespace Flow.Launcher { public partial class ProgressBoxEx : Window { + private static readonly string ClassName = nameof(ProgressBoxEx); + private readonly Action _cancelProgress; private ProgressBoxEx(Action cancelProgress) @@ -47,7 +48,7 @@ namespace Flow.Launcher } catch (Exception e) { - Log.Error($"|ProgressBoxEx.Show|An error occurred: {e.Message}"); + App.API.LogError(ClassName, $"An error occurred: {e.Message}"); await reportProgressAsync(null).ConfigureAwait(false); } diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index 5438eac7d..3abc57b8a 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -435,16 +435,16 @@ namespace Flow.Launcher PluginManager.UninstallPluginAsync(pluginMetadata, removePluginSettings); public long StopwatchLogDebug(string className, string message, Action action, [CallerMemberName] string methodName = "") => - Stopwatch.Debug($"|{className}.{methodName}|{message}", action); + Stopwatch.Debug(className, message, action, methodName); public Task StopwatchLogDebugAsync(string className, string message, Func action, [CallerMemberName] string methodName = "") => - Stopwatch.DebugAsync($"|{className}.{methodName}|{message}", action); + Stopwatch.DebugAsync(className, message, action, methodName); public long StopwatchLogInfo(string className, string message, Action action, [CallerMemberName] string methodName = "") => - Stopwatch.Normal($"|{className}.{methodName}|{message}", action); + Stopwatch.Info(className, message, action, methodName); public Task StopwatchLogInfoAsync(string className, string message, Func action, [CallerMemberName] string methodName = "") => - Stopwatch.NormalAsync($"|{className}.{methodName}|{message}", action); + Stopwatch.InfoAsync(className, message, action, methodName); #endregion diff --git a/Flow.Launcher/ReportWindow.xaml.cs b/Flow.Launcher/ReportWindow.xaml.cs index 6fe90783e..0ab785a17 100644 --- a/Flow.Launcher/ReportWindow.xaml.cs +++ b/Flow.Launcher/ReportWindow.xaml.cs @@ -8,13 +8,15 @@ using System.Windows; using System.Windows.Documents; using Flow.Launcher.Helper; using Flow.Launcher.Infrastructure; -using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Plugin.SharedCommands; +using Flow.Launcher.Infrastructure.UserSettings; namespace Flow.Launcher { internal partial class ReportWindow { + private static readonly string ClassName = nameof(ReportWindow); + public ReportWindow(Exception exception) { InitializeComponent(); @@ -38,7 +40,7 @@ namespace Flow.Launcher private void SetException(Exception exception) { - string path = Log.CurrentLogDirectory; + var path = DataLocation.VersionLogDirectory; var directory = new DirectoryInfo(path); var log = directory.GetFiles().OrderByDescending(f => f.LastWriteTime).First(); diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index f6b9aa67c..998fdb906 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -16,7 +16,6 @@ using CommunityToolkit.Mvvm.Input; using Flow.Launcher.Core.Plugin; using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure.Hotkey; -using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.Storage; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; @@ -30,6 +29,8 @@ namespace Flow.Launcher.ViewModel { #region Private Fields + private static readonly string ClassName = nameof(MainViewModel); + private bool _isQueryRunning; private Query _lastQuery; private string _queryTextBeforeLeaveResults; @@ -213,7 +214,7 @@ namespace Flow.Launcher.ViewModel } if (!_disposed) - Log.Error("MainViewModel", "Unexpected ResultViewUpdate ends"); + App.API.LogError(ClassName, "Unexpected ResultViewUpdate ends"); } void continueAction(Task t) @@ -257,7 +258,7 @@ namespace Flow.Launcher.ViewModel if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, pair.Metadata, e.Query, token))) { - Log.Error("MainViewModel", "Unable to add item to Result Update Queue"); + App.API.LogError(ClassName, "Unable to add item to Result Update Queue"); } }; } @@ -1303,7 +1304,7 @@ namespace Flow.Launcher.ViewModel if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, plugin.Metadata, query, token, reSelect))) { - Log.Error("MainViewModel", "Unable to add item to Result Update Queue"); + App.API.LogError(ClassName, "Unable to add item to Result Update Queue"); } } } @@ -1347,8 +1348,8 @@ namespace Flow.Launcher.ViewModel } catch (Exception e) { - Log.Exception( - $"{nameof(MainViewModel)}.{nameof(ConstructQuery)}|Error when expanding shortcut {shortcut.Key}", + App.API.LogException(ClassName, + $"Error when expanding shortcut {shortcut.Key}", e); } } diff --git a/Flow.Launcher/ViewModel/ResultViewModel.cs b/Flow.Launcher/ViewModel/ResultViewModel.cs index 8d7569dc1..ef7e24c3b 100644 --- a/Flow.Launcher/ViewModel/ResultViewModel.cs +++ b/Flow.Launcher/ViewModel/ResultViewModel.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Generic; using System.Drawing.Text; using System.IO; @@ -7,7 +6,6 @@ using System.Threading.Tasks; using System.Windows; using System.Windows.Media; using Flow.Launcher.Infrastructure.Image; -using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; @@ -15,6 +13,8 @@ namespace Flow.Launcher.ViewModel { public class ResultViewModel : BaseModel { + private static readonly string ClassName = nameof(ResultsViewModel); + private static readonly PrivateFontCollection FontCollection = new(); private static readonly Dictionary Fonts = new(); @@ -232,8 +232,8 @@ namespace Flow.Launcher.ViewModel } catch (Exception e) { - Log.Exception( - $"|ResultViewModel.LoadImageInternalAsync|IcoPath is empty and exception when calling IconDelegate for result <{Result.Title}> of plugin <{Result.PluginDirectory}>", + App.API.LogException(ClassName, + $"IcoPath is empty and exception when calling IconDelegate for result <{Result.Title}> of plugin <{Result.PluginDirectory}>", e); } }