From 045fd20d8c41061bcb5a4f1f80e757b7199c9a81 Mon Sep 17 00:00:00 2001 From: bao-qian Date: Tue, 24 Jan 2017 00:24:20 +0000 Subject: [PATCH] Rewrite all log message format --- Plugins/Wox.Plugin.PluginManagement/Main.cs | 17 +- .../Wox.Plugin.Program/FileChangeWatcher.cs | 2 +- Plugins/Wox.Plugin.Program/Main.cs | 9 +- Plugins/Wox.Plugin.Program/Programs/UWP.cs | 25 ++- Plugins/Wox.Plugin.Program/Programs/Win32.cs | 10 +- Plugins/Wox.Plugin.Shell/Main.cs | 2 +- .../SuggestionSources/Baidu.cs | 6 +- .../SuggestionSources/Google.cs | 5 +- Wox.Core/Plugin/JsonRPCPlugin.cs | 2 +- Wox.Core/Plugin/PluginConfig.cs | 13 +- Wox.Core/Plugin/PluginManager.cs | 8 +- Wox.Core/Plugin/PluginsLoader.cs | 14 +- Wox.Core/Resource/Internationalization.cs | 5 +- Wox.Core/Resource/Theme.cs | 2 +- Wox.Core/Updater.cs | 20 +-- Wox.Infrastructure/Image/ImageLoader.cs | 6 +- Wox.Infrastructure/Logger/Log.cs | 167 +++++++++--------- Wox.Infrastructure/Stopwatch.cs | 14 +- Wox.Infrastructure/Storage/BinaryStorage.cs | 12 +- Wox.Infrastructure/Storage/JsonStorage.cs | 2 +- Wox/App.xaml.cs | 9 +- Wox/Helper/ErrorReporting.cs | 5 +- Wox/ViewModel/ResultViewModel.cs | 2 +- Wox/Wox.csproj | 8 +- Wox/packages.config | 1 + 25 files changed, 180 insertions(+), 186 deletions(-) diff --git a/Plugins/Wox.Plugin.PluginManagement/Main.cs b/Plugins/Wox.Plugin.PluginManagement/Main.cs index eba7618251..2ad1e40fab 100644 --- a/Plugins/Wox.Plugin.PluginManagement/Main.cs +++ b/Plugins/Wox.Plugin.PluginManagement/Main.cs @@ -117,8 +117,9 @@ namespace Wox.Plugin.PluginManagement } catch (WebException e) { - Log.Warn("Can't connect to Wox plugin website, check your conenction"); - Log.Exception(e); + //todo happlebao add option in log to decide give user prompt or not + context.API.ShowMsg("PluginManagement.ResultForInstallPlugin: Can't connect to Wox plugin website, check your conenction"); + Log.Exception("|PluginManagement.ResultForInstallPlugin|Can't connect to Wox plugin website, check your conenction", e); return new List(); } List searchedPlugins; @@ -126,10 +127,10 @@ namespace Wox.Plugin.PluginManagement { searchedPlugins = JsonConvert.DeserializeObject>(json); } - catch(JsonSerializationException e) + catch (JsonSerializationException e) { - context.API.ShowMsg("Coundn't parse api search results", "Please update your Wox!", string.Empty); - Log.Exception(e); + context.API.ShowMsg("PluginManagement.ResultForInstallPlugin: Coundn't parse api search results, Please update your Wox!"); + Log.Exception("|PluginManagement.ResultForInstallPlugin|Coundn't parse api search results, Please update your Wox!", e); return results; } @@ -160,10 +161,8 @@ namespace Wox.Plugin.PluginManagement } catch (WebException e) { - var info = "download plugin " + r.name + "failed."; - MessageBox.Show(info); - Log.Warn(info); - Log.Exception(e); + context.API.ShowMsg($"PluginManagement.ResultForInstallPlugin: download failed for <{r.name}>"); + Log.Exception($"|PluginManagement.ResultForInstallPlugin|download failed for <{r.name}>", e); return false; } context.API.InstallPlugin(filePath); diff --git a/Plugins/Wox.Plugin.Program/FileChangeWatcher.cs b/Plugins/Wox.Plugin.Program/FileChangeWatcher.cs index c3968f2df4..0df361215c 100644 --- a/Plugins/Wox.Plugin.Program/FileChangeWatcher.cs +++ b/Plugins/Wox.Plugin.Program/FileChangeWatcher.cs @@ -26,7 +26,7 @@ namespace Wox.Plugin.Program // if (WatchedPath.Contains(path)) return; // if (!Directory.Exists(path)) // { - // Log.Warn($"FileChangeWatcher: {path} doesn't exist"); + // Log.Warn($"|FileChangeWatcher|{path} doesn't exist"); // return; // } diff --git a/Plugins/Wox.Plugin.Program/Main.cs b/Plugins/Wox.Plugin.Program/Main.cs index 139a9b01c5..a0040d490f 100644 --- a/Plugins/Wox.Plugin.Program/Main.cs +++ b/Plugins/Wox.Plugin.Program/Main.cs @@ -31,19 +31,18 @@ namespace Wox.Plugin.Program _settingsStorage = new PluginJsonStorage(); _settings = _settingsStorage.Load(); - Stopwatch.Normal("Preload programs", () => + Stopwatch.Normal("|Wox.Plugin.Program.Main|Preload programs cost", () => { _win32Storage = new BinaryStorage("Win32"); _win32s = _win32Storage.TryLoad(new Win32[] { }); _uwpStorage = new BinaryStorage("UWP"); _uwps = _uwpStorage.TryLoad(new UWP.Application[] { }); - }); - Log.Info($"Preload {_win32s.Length} win32 programs from cache"); - Log.Info($"Preload {_uwps.Length} uwps from cache"); + Log.Info($"|Wox.Plugin.Program.Main|Number of preload win32 programs<{_win32s.Length}>"); + Log.Info($"|Wox.Plugin.Program.Main|Number of preload uwps <{_uwps.Length}>"); Task.Run(() => { - Stopwatch.Normal("Program Index", IndexPrograms); + Stopwatch.Normal("|Wox.Plugin.Program.Main|Program index cost", IndexPrograms); }); } diff --git a/Plugins/Wox.Plugin.Program/Programs/UWP.cs b/Plugins/Wox.Plugin.Program/Programs/UWP.cs index 55758e3707..751afaa96d 100644 --- a/Plugins/Wox.Plugin.Program/Programs/UWP.cs +++ b/Plugins/Wox.Plugin.Program/Programs/UWP.cs @@ -35,7 +35,7 @@ namespace Wox.Plugin.Program.Programs public UWP(Package package) { - + Location = package.InstalledLocation.Path; Name = package.Id.Name; FullName = package.Id.FullName; @@ -83,9 +83,8 @@ namespace Wox.Plugin.Program.Programs } else { - Log.Error($"SHCreateStreamOnFileEx on path: <{path}> failed, HResult error code: {hResult}. Package location: <{Location}>."); - var exception = Marshal.GetExceptionForHR((int)hResult); - Log.Exception(exception); + var e = Marshal.GetExceptionForHR((int)hResult); + Log.Exception($"|UWP.InitializeAppInfo|SHCreateStreamOnFileEx on path <{path}> failed with HResult <{hResult}> and location <{Location}>.", e); } } @@ -109,7 +108,7 @@ namespace Wox.Plugin.Program.Programs } else { - Log.Error($"can't find namespaces for <{path}>"); + Log.Error($"|UWP.XmlNamespaces|can't find namespaces for <{path}>"); return new string[] { }; } } @@ -132,7 +131,7 @@ namespace Wox.Plugin.Program.Programs } } - Log.Error($"Unknown Appmanifest version: {FullName}, Package location: <{Location}>."); + Log.Error($"|UWP.InitPackageVersion| Unknown Appmanifest version UWP <{FullName}> with location <{Location}>."); Version = PackageVersion.Unknown; } @@ -217,7 +216,7 @@ namespace Wox.Plugin.Program.Programs var score1 = StringMatcher.Score(DisplayName, query); var score2 = StringMatcher.ScoreForPinyin(DisplayName, query); var score3 = StringMatcher.Score(Description, query); - var score4= StringMatcher.ScoreForPinyin(Description, query); + var score4 = StringMatcher.ScoreForPinyin(Description, query); var score = new[] { score1, score2, score3, score4 }.Max(); return score; } @@ -341,7 +340,7 @@ namespace Wox.Plugin.Program.Programs } else { - Log.Error($"Load {source} failed, null or empty result. Package location: <{Package.Location}>."); + Log.Error($"|UWP.ResourceFromPri|Can't load null or empty result pri <{source}> with uwp location <{Package.Location}>."); return string.Empty; } } @@ -353,7 +352,7 @@ namespace Wox.Plugin.Program.Programs // Microsoft.MicrosoftOfficeHub_17.7608.23501.0_x64__8wekyb3d8bbwe: ms-resource://Microsoft.MicrosoftOfficeHub/officehubintl/AppManifest_GetOffice_Description // Microsoft.BingFoodAndDrink_3.0.4.336_x64__8wekyb3d8bbwe: ms-resource:AppDescription var e = Marshal.GetExceptionForHR((int)hResult); - Log.Error(e, $"Load {source} failed, HResult error code: {hResult}. Package location: <{Package.Location}>."); + Log.Exception($"|UWP.ResourceFromPri|Load pri failed <{source}> with HResult <{hResult}> and location <{Package.Location}>.", e); return string.Empty; } } @@ -435,13 +434,13 @@ namespace Wox.Plugin.Program.Programs } else { - Log.Error($"<{UserModelId}> can't find logo uri: <{uri}>, Package location: <{Package.Location}>."); + Log.Error($"|UWP.LogoPathFromUri| <{UserModelId}> can't find logo uri for <{uri}>, Package location <{Package.Location}>."); return string.Empty; } } else { - Log.Error($"<{UserModelId}> cantains uri doesn't have extension: <{uri}>, Package location: <{Package.Location}>."); + Log.Error($"|UWP.LogoPathFromUri| <{UserModelId}> cantains can't find extension for <{uri}> Package location <{Package.Location}>."); return string.Empty; } } @@ -467,7 +466,7 @@ namespace Wox.Plugin.Program.Programs } else { - Log.Error($"Can't get logo for <{UserModelId}> with path <{path}>, Package location: <{Package.Location}>.."); + Log.Error($"|UWP.ImageFromPath|Can't get logo for <{UserModelId}> with path <{path}> and location <{Package.Location}>"); return new BitmapImage(new Uri(Constant.ErrorIcon)); } } @@ -514,7 +513,7 @@ namespace Wox.Plugin.Program.Programs } else { - Log.Error($"Can't convert background string <{BackgroundColor}> to color, Package location: <{Package.Location}>."); + Log.Error($"|UWP.PlatedImage| Can't convert background string <{BackgroundColor}> to color for <{Package.Location}>."); return new BitmapImage(new Uri(Constant.ErrorIcon)); } } diff --git a/Plugins/Wox.Plugin.Program/Programs/Win32.cs b/Plugins/Wox.Plugin.Program/Programs/Win32.cs index f1dd0a4d55..81a556c432 100644 --- a/Plugins/Wox.Plugin.Program/Programs/Win32.cs +++ b/Plugins/Wox.Plugin.Program/Programs/Win32.cs @@ -140,7 +140,7 @@ namespace Wox.Plugin.Program.Programs { var link = new ShellLink(); const uint STGM_READ = 0; - ((IPersistFile) link).Load(path, STGM_READ); + ((IPersistFile)link).Load(path, STGM_READ); var hwnd = new _RemotableHandle(); link.Resolve(ref hwnd, 0); @@ -178,15 +178,13 @@ namespace Wox.Plugin.Program.Programs catch (COMException e) { // C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\MiracastView.lnk always cause exception - Log.Error($"COMException when parsing shortcut: {path}, HResult: {e.HResult}"); - Log.Exception(e); + Log.Exception($"|Win32.LnkProgram|COMException when parsing shortcut <{path}> with HResult <{e.HResult}>", e); program.Valid = false; return program; } catch (Exception e) { - Log.Error($"Error when parsing shortcut: {path}"); - Log.Exception(e); + Log.Exception($"|Win32.LnkProgram|Exception when parsing shortcut <{path}>", e); program.Valid = false; return program; } @@ -301,7 +299,7 @@ namespace Wox.Plugin.Program.Programs if (!string.IsNullOrEmpty(path)) { // fix path like this: ""\"C:\\folder\\executable.exe\"" - path = path.Trim('"'); + path = path.Trim('"', ' '); path = Environment.ExpandEnvironmentVariables(path); if (File.Exists(path)) diff --git a/Plugins/Wox.Plugin.Shell/Main.cs b/Plugins/Wox.Plugin.Shell/Main.cs index 360ba508b9..1e60ef8c9c 100644 --- a/Plugins/Wox.Plugin.Shell/Main.cs +++ b/Plugins/Wox.Plugin.Shell/Main.cs @@ -91,7 +91,7 @@ namespace Wox.Plugin.Shell } catch (Exception e) { - Log.Exception(e); + Log.Exception($"|Wox.Plugin.Shell.Main.Query|Exception when query for <{query}>", e); } return results; } diff --git a/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Baidu.cs b/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Baidu.cs index 0c71d7a2b4..7d58a57f5c 100644 --- a/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Baidu.cs +++ b/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Baidu.cs @@ -26,10 +26,8 @@ namespace Wox.Plugin.WebSearch.SuggestionSources } catch (WebException e) { - Log.Warn("Can't get suggestion from baidu"); - Log.Exception(e); + Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e); return new List(); - ; } if (string.IsNullOrEmpty(result)) return new List(); @@ -43,7 +41,7 @@ namespace Wox.Plugin.WebSearch.SuggestionSources } catch (JsonSerializationException e) { - Log.Exception(e); + Log.Exception("|Baidu.Suggestions|can't parse suggestions", e); return new List(); } diff --git a/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Google.cs b/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Google.cs index 6d1f9eaa94..d7a908969b 100644 --- a/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Google.cs +++ b/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Google.cs @@ -22,8 +22,7 @@ namespace Wox.Plugin.WebSearch.SuggestionSources } catch (WebException e) { - Log.Warn("Can't get suggestion from google"); - Log.Exception(e); + Log.Exception("|Google.Suggestions|Can't get suggestion from google", e); return new List(); ; } @@ -35,7 +34,7 @@ namespace Wox.Plugin.WebSearch.SuggestionSources } catch (JsonSerializationException e) { - Log.Exception(e); + Log.Exception("|Google.Suggestions|can't parse suggestions", e); return new List(); } if (json != null) diff --git a/Wox.Core/Plugin/JsonRPCPlugin.cs b/Wox.Core/Plugin/JsonRPCPlugin.cs index f83278940c..f781d3329f 100644 --- a/Wox.Core/Plugin/JsonRPCPlugin.cs +++ b/Wox.Core/Plugin/JsonRPCPlugin.cs @@ -75,7 +75,7 @@ namespace Wox.Core.Plugin } catch (Exception e) { - Log.Exception(e); + Log.Exception($"|Wox.Core.Plugin.JsonRPCPlugin.Query|Exception when query <{query}>", e); } } return null; diff --git a/Wox.Core/Plugin/PluginConfig.cs b/Wox.Core/Plugin/PluginConfig.cs index 0b59c06f51..8721a85a1c 100644 --- a/Wox.Core/Plugin/PluginConfig.cs +++ b/Wox.Core/Plugin/PluginConfig.cs @@ -42,7 +42,7 @@ namespace Wox.Core.Plugin } catch (Exception e) { - Log.Fatal(e); + Log.Exception($"|PluginConfig.ParsePLuginConfigs|Can't delete <{directory}>", e); } } else @@ -61,7 +61,7 @@ namespace Wox.Core.Plugin string configPath = Path.Combine(pluginDirectory, PluginConfigName); if (!File.Exists(configPath)) { - Log.Warn($"parse plugin {configPath} failed: didn't find config file."); + Log.Error($"|PluginConfig.GetPluginMetadata|Didn't find config file <{configPath}>"); return null; } @@ -77,23 +77,20 @@ namespace Wox.Core.Plugin } catch (Exception e) { - string msg = $"Parse plugin config {configPath} failed: json format is not valid"; - Log.Exception(new WoxException(msg)); + Log.Exception($"|PluginConfig.GetPluginMetadata|invalid json for config <{configPath}>", e); return null; } if (!AllowedLanguage.IsAllowed(metadata.Language)) { - string msg = $"Parse plugin config {configPath} failed: invalid language {metadata.Language}"; - Log.Exception(new WoxException(msg)); + Log.Error($"|PluginConfig.GetPluginMetadata|Invalid language <{metadata.Language}> for config <{configPath}>"); return null; } if (!File.Exists(metadata.ExecuteFilePath)) { - string msg = $"Parse plugin config {configPath} failed: ExecuteFile {metadata.ExecuteFilePath} didn't exist"; - Log.Exception(new WoxException(msg)); + Log.Error($"|PluginConfig.GetPluginMetadata|execute file path didn't exist <{metadata.ExecuteFilePath}> for conifg <{configPath}"); return null; } diff --git a/Wox.Core/Plugin/PluginManager.cs b/Wox.Core/Plugin/PluginManager.cs index 03701f84f7..0e3a0a4774 100644 --- a/Wox.Core/Plugin/PluginManager.cs +++ b/Wox.Core/Plugin/PluginManager.cs @@ -93,7 +93,7 @@ namespace Wox.Core.Plugin API = api; Parallel.ForEach(AllPlugins, pair => { - var milliseconds = Stopwatch.Debug($"Plugin init: {pair.Metadata.Name}", () => + var milliseconds = Stopwatch.Debug($"|PluginManager.InitializePlugins|Init method time cost for <{pair.Metadata.Name}>", () => { pair.Plugin.Init(new PluginInitContext { @@ -102,7 +102,7 @@ namespace Wox.Core.Plugin }); }); pair.Metadata.InitTime += milliseconds; - Log.Info($"Total init for {pair.Metadata.Name}: {pair.Metadata.InitTime}ms"); + Log.Info($"|PluginManager.InitializePlugins|Total init cost for <{pair.Metadata.Name}> is <{pair.Metadata.InitTime}ms>"); InternationalizationManager.Instance.UpdatePluginMetadataTranslations(pair); }); @@ -177,7 +177,7 @@ namespace Wox.Core.Plugin try { var metadata = pair.Metadata; - var milliseconds = Stopwatch.Debug($"Plugin.Query cost for {metadata.Name}", () => + var milliseconds = Stopwatch.Debug($"|PluginManager.QueryForPlugin|Cost for {metadata.Name}", () => { results = pair.Plugin.Query(query) ?? results; UpdatePluginMetadata(results, metadata, query); @@ -243,7 +243,7 @@ namespace Wox.Core.Plugin } catch (Exception e) { - Log.Exception(new WoxPluginException(metadata.Name, "Couldn't load plugin context menus", e)); + Log.Exception($"|PluginManager.GetContextMenusForPlugin|Can't load context menus for plugin <{metadata.Name}>", e); return new List(); } } diff --git a/Wox.Core/Plugin/PluginsLoader.cs b/Wox.Core/Plugin/PluginsLoader.cs index 8f37a63525..3f5f9ad320 100644 --- a/Wox.Core/Plugin/PluginsLoader.cs +++ b/Wox.Core/Plugin/PluginsLoader.cs @@ -33,7 +33,7 @@ namespace Wox.Core.Plugin foreach (var metadata in metadatas) { - var milliseconds = Stopwatch.Debug($"C# plugin constructor init: {metadata.Name}", () => + var milliseconds = Stopwatch.Debug($"|PluginsLoader.CSharpPlugins|Constructor init cost for {metadata.Name}", () => { #if DEBUG @@ -49,7 +49,7 @@ namespace Wox.Core.Plugin } catch (Exception e) { - Log.Exception(new WoxPluginException(metadata.Name, "Couldn't load assembly", e)); + Log.Exception($"|PluginsLoader.CSharpPlugins|Couldn't load assembly for {metadata.Name}", e); return; } var types = assembly.GetTypes(); @@ -60,7 +60,7 @@ namespace Wox.Core.Plugin } catch (InvalidOperationException e) { - Log.Exception(new WoxPluginException(metadata.Name, "Can't find class implement IPlugin", e)); + Log.Exception($"|PluginsLoader.CSharpPlugins|Can't find class implement IPlugin for <{metadata.Name}>", e); return; } IPlugin plugin; @@ -70,7 +70,7 @@ namespace Wox.Core.Plugin } catch (Exception e) { - Log.Exception(new WoxPluginException(metadata.Name, "Can't create instance", e)); + Log.Exception($"|PluginsLoader.CSharpPlugins|Can't create instance for <{metadata.Name}>", e); return; } #endif @@ -104,13 +104,13 @@ namespace Wox.Core.Plugin } else { - Log.Exception(new WoxException("Python can't be found in PATH.")); + Log.Error("|PluginsLoader.PythonPlugins|Python can't be found in PATH."); return new List(); } } else { - Log.Exception(new WoxException("Path variable is not set.")); + Log.Error("|PluginsLoader.PythonPlugins|PATH environment variable is not set."); return new List(); } } @@ -123,7 +123,7 @@ namespace Wox.Core.Plugin } else { - Log.Exception(new WoxException("Can't find python executable in python directory")); + Log.Error("|PluginsLoader.PythonPlugins|Can't find python executable in (); } } diff --git a/Wox.Core/Resource/Internationalization.cs b/Wox.Core/Resource/Internationalization.cs index 4da7abd97c..b6e237ec26 100644 --- a/Wox.Core/Resource/Internationalization.cs +++ b/Wox.Core/Resource/Internationalization.cs @@ -30,7 +30,7 @@ namespace Wox.Core.Resource } catch (Exception e) { - Log.Exception(e); + Log.Exception($"|Internationalization.MakesureDirectoriesExist|Exception when create directory <{DirectoryPath}>", e); } } } @@ -117,8 +117,7 @@ namespace Wox.Core.Resource } catch (Exception e) { - var woxPluginException = new WoxPluginException(pluginPair.Metadata.Name, "Update Plugin metadata translation failed:", e); - Log.Exception(woxPluginException); + Log.Exception($"|Internationalization.UpdatePluginMetadataTranslations|Update Plugin metadata translation failed for <{pluginPair.Metadata.Name}>", e); } } diff --git a/Wox.Core/Resource/Theme.cs b/Wox.Core/Resource/Theme.cs index 68df370b1f..59efde70f7 100644 --- a/Wox.Core/Resource/Theme.cs +++ b/Wox.Core/Resource/Theme.cs @@ -36,7 +36,7 @@ namespace Wox.Core.Resource } catch (Exception e) { - Log.Exception(e); + Log.Exception($"|Theme.MakesureThemeDirectoriesExist|Exception when create directory <{pluginDirectory}>", e); } } } diff --git a/Wox.Core/Updater.cs b/Wox.Core/Updater.cs index ee51385399..e5a97ea96e 100644 --- a/Wox.Core/Updater.cs +++ b/Wox.Core/Updater.cs @@ -18,7 +18,7 @@ namespace Wox.Core public static async void UpdateApp() { - var client = new WebClient {Proxy = Http.WebProxy()}; + var client = new WebClient { Proxy = Http.WebProxy() }; var downloader = new FileDownloader(client); try @@ -33,23 +33,22 @@ namespace Wox.Core } catch (HttpRequestException he) { - Log.Exception(he); + Log.Exception("|Updater.UpdateApp|network error", he); } catch (WebException we) { - Log.Exception(we); + Log.Exception("|Updater.UpdateApp|network error", we); } catch (SocketException sc) { - Log.Info("Socket exception happened!, which method cause this exception??"); - Log.Exception(sc); + Log.Exception("|Updater.UpdateApp|Socket exception happened, which method cause this exception??", sc); } catch (Exception exception) { const string info = "Update.exe not found, not a Squirrel-installed app?"; if (exception.Message == info) { - Log.Warn(info); + Log.Warn($"|Updater.UpdateApp|{info}"); } else { @@ -70,8 +69,7 @@ namespace Wox.Core } catch (WebException e) { - Log.Warn("Can't connect to github api to check new version"); - Log.Exception(e); + Log.Exception("|Updater.NewVersion|Can't connect to github api to check new version", e); return string.Empty; } @@ -84,7 +82,7 @@ namespace Wox.Core } catch (JsonSerializationException e) { - Log.Exception(e); + Log.Exception("|Updater.NewVersion|can't parse response", e); return string.Empty; } var version = json?["tag_name"]?.ToString(); @@ -94,13 +92,13 @@ namespace Wox.Core } else { - Log.Warn("Can't find tag_name from Github API response"); + Log.Warn("|Updater.NewVersion|Can't find tag_name from Github API response"); return string.Empty; } } else { - Log.Warn("Can't get response from Github API"); + Log.Warn("|Updater.NewVersion|Can't get response from Github API"); return string.Empty; } } diff --git a/Wox.Infrastructure/Image/ImageLoader.cs b/Wox.Infrastructure/Image/ImageLoader.cs index dce0081f7d..d99cf789d2 100644 --- a/Wox.Infrastructure/Image/ImageLoader.cs +++ b/Wox.Infrastructure/Image/ImageLoader.cs @@ -76,7 +76,7 @@ namespace Wox.Infrastructure.Image } catch (System.Exception e) { - Log.Exception(e); + Log.Exception($"|ImageLoader.ShellIcon|can't get shell icon for <{fileName}>", e); return ImageCache[Constant.ErrorIcon]; } } @@ -91,7 +91,7 @@ namespace Wox.Infrastructure.Image } Task.Run(() => { - Stopwatch.Normal("Preload images from cache", () => + Stopwatch.Normal("|ImageLoader.PreLoadImages|Preload images cost", () => { ImageCache.Usage.AsParallel().Where(i => !ImageCache.ContainsKey(i.Key)).ForAll(i => { @@ -102,7 +102,7 @@ namespace Wox.Infrastructure.Image } }); }); - Log.Info($"Preload {ImageCache.Usage.Count} images from cache"); + Log.Info($"|ImageLoader.PreLoadImages|Number of preload images is <{ImageCache.Usage.Count}>"); }); } diff --git a/Wox.Infrastructure/Logger/Log.cs b/Wox.Infrastructure/Logger/Log.cs index 7e9f8acdb7..d8a5f1a141 100644 --- a/Wox.Infrastructure/Logger/Log.cs +++ b/Wox.Infrastructure/Logger/Log.cs @@ -4,7 +4,6 @@ using System.Runtime.CompilerServices; using NLog; using NLog.Config; using NLog.Targets; -using Wox.Infrastructure.Exception; namespace Wox.Infrastructure.Logger { @@ -34,98 +33,104 @@ namespace Wox.Infrastructure.Logger LogManager.Configuration = configuration; } - public static string CallerType() + private static void LogFaultyFormat(string message) { - var stackTrace = new StackTrace(); - var stackFrames = stackTrace.GetFrames().NonNull(); - var callingFrame = stackFrames[2]; - var method = callingFrame.GetMethod(); - var type = $"{method.DeclaringType.NonNull().FullName}.{method.Name}"; - return type; + var logger = LogManager.GetLogger("FaultyLogger"); + message = $"Wrong logger message format <{message}>"; + System.Diagnostics.Debug.WriteLine($"FATAL|{message}"); + logger.Fatal(message); } - public static void Error(string msg) + public static void Error(string message) { - var type = CallerType(); - var logger = LogManager.GetLogger(type); - System.Diagnostics.Debug.WriteLine($"ERROR: {msg}"); - logger.Error(msg); + var parts = message.Split('|'); + if (parts.Length == 3 && !string.IsNullOrWhiteSpace(parts[1]) && !string.IsNullOrWhiteSpace(parts[2])) + { + var logger = LogManager.GetLogger(parts[1]); + System.Diagnostics.Debug.WriteLine($"ERROR|{message}"); + logger.Error(parts[2]); + } + else + { + LogFaultyFormat(message); + } } [MethodImpl(MethodImplOptions.Synchronized)] - public static void Error(System.Exception e, string msg) - { - var type = CallerType(); - var logger = LogManager.GetLogger(type); - System.Diagnostics.Debug.WriteLine($"ERROR: {msg}"); - logger.Error("-------------------------- Begin exception --------------------------"); - logger.Error(msg); - do - { - logger.Error($"Exception message:\n <{e.Message}>"); - logger.Error($"Exception stack trace:\n<{e.StackTrace}>"); - e = e.InnerException; - } while (e != null); - logger.Error("-------------------------- End exception --------------------------"); - } - - [MethodImpl(MethodImplOptions.Synchronized)] - public static void Exception(System.Exception e) - { - var type = CallerType(); - var logger = LogManager.GetLogger(type); - - do - { - logger.Error($"Exception message:\n <{e.Message}>"); - logger.Error($"Exception stack trace:\n<{e.StackTrace}>"); - e = e.InnerException; - } while (e != null); - } - - public static void Debug(string type, string msg) - { - var logger = LogManager.GetLogger(type); - System.Diagnostics.Debug.WriteLine($"DEBUG: {msg}"); - logger.Debug(msg); - } - - public static void Debug(string msg) - { - var type = CallerType(); - Debug(type, msg); - } - - public static void Info(string type, string msg) - { - var logger = LogManager.GetLogger(type); - System.Diagnostics.Debug.WriteLine($"INFO: {msg}"); - logger.Info(msg); - } - - public static void Info(string msg) - { - var type = CallerType(); - Info(type, msg); - } - - public static void Warn(string msg) - { - var type = CallerType(); - var logger = LogManager.GetLogger(type); - System.Diagnostics.Debug.WriteLine($"WARN: {msg}"); - logger.Warn(msg); - } - - public static void Fatal(System.Exception e) + public static void Exception(string message, System.Exception e) { #if DEBUG throw e; #else - var type = CallerType(); - var logger = LogManager.GetLogger(type); - logger.Fatal(ExceptionFormatter.FormatExcpetion(e)); + var parts = message.Split('|'); + if (parts.Length == 3 && !string.IsNullOrWhiteSpace(parts[1]) && !string.IsNullOrWhiteSpace(parts[2])) + { + var logger = LogManager.GetLogger(parts[1]); + + System.Diagnostics.Debug.WriteLine($"ERROR|{message}"); + + logger.Error("-------------------------- Begin exception --------------------------"); + logger.Error(parts[2]); + + do + { + logger.Error($"Exception message:\n <{e.Message}>"); + logger.Error($"Exception stack trace:\n<{e.StackTrace}>"); + e = e.InnerException; + } while (e != null); + + logger.Error("-------------------------- End exception --------------------------"); + } + else + { + LogFaultyFormat(message); + } #endif } + + public static void Debug(string message) + { + var parts = message.Split('|'); + if (parts.Length == 3 && !string.IsNullOrWhiteSpace(parts[1]) && !string.IsNullOrWhiteSpace(parts[2])) + { + var logger = LogManager.GetLogger(parts[1]); + System.Diagnostics.Debug.WriteLine($"DEBUG|{message}"); + logger.Debug(parts[2]); + } + else + { + LogFaultyFormat(message); + } + } + + public static void Info(string message) + { + var parts = message.Split('|'); + if (parts.Length == 3 && !string.IsNullOrWhiteSpace(parts[1]) && !string.IsNullOrWhiteSpace(parts[2])) + { + var logger = LogManager.GetLogger(parts[1]); + System.Diagnostics.Debug.WriteLine($"INFO|{message}"); + logger.Info(parts[2]); + } + else + { + LogFaultyFormat(message); + } + } + + public static void Warn(string message) + { + var parts = message.Split('|'); + if (parts.Length == 3 && !string.IsNullOrWhiteSpace(parts[1]) && !string.IsNullOrWhiteSpace(parts[2])) + { + var logger = LogManager.GetLogger(parts[1]); + System.Diagnostics.Debug.WriteLine($"WARN|{message}"); + logger.Warn(parts[2]); + } + else + { + LogFaultyFormat(message); + } + } } } \ No newline at end of file diff --git a/Wox.Infrastructure/Stopwatch.cs b/Wox.Infrastructure/Stopwatch.cs index d9c11af469..6315654552 100644 --- a/Wox.Infrastructure/Stopwatch.cs +++ b/Wox.Infrastructure/Stopwatch.cs @@ -11,29 +11,27 @@ namespace Wox.Infrastructure /// /// This stopwatch will appear only in Debug mode /// - public static long Debug(string name, Action action) + public static long Debug(string message, Action action) { var stopWatch = new System.Diagnostics.Stopwatch(); stopWatch.Start(); action(); stopWatch.Stop(); var milliseconds = stopWatch.ElapsedMilliseconds; - string info = $"{name} : {milliseconds}ms"; - var type = Log.CallerType(); - Log.Debug(type, info); + string info = $"{message} <{milliseconds}ms>"; + Log.Debug(info); return milliseconds; } - public static long Normal(string name, Action action) + public static long Normal(string message, Action action) { var stopWatch = new System.Diagnostics.Stopwatch(); stopWatch.Start(); action(); stopWatch.Stop(); var milliseconds = stopWatch.ElapsedMilliseconds; - string info = $"{name} : {milliseconds}ms"; - var type = Log.CallerType(); - Log.Info(type, info); + string info = $"{message} <{milliseconds}ms>"; + Log.Info(info); return milliseconds; } diff --git a/Wox.Infrastructure/Storage/BinaryStorage.cs b/Wox.Infrastructure/Storage/BinaryStorage.cs index 190e8ff7ec..5c787f83b5 100644 --- a/Wox.Infrastructure/Storage/BinaryStorage.cs +++ b/Wox.Infrastructure/Storage/BinaryStorage.cs @@ -39,7 +39,7 @@ namespace Wox.Infrastructure.Storage } else { - Log.Error($"Zero length cache file: {FilePath}"); + Log.Error($"|BinaryStorage.TryLoad|Zero length cache file <{FilePath}>"); Save(defaultData); return defaultData; } @@ -47,7 +47,7 @@ namespace Wox.Infrastructure.Storage } else { - Log.Info("Cache file not exist, load default data"); + Log.Info("|BinaryStorage.TryLoad|Cache file not exist, load default data"); Save(defaultData); return defaultData; } @@ -64,13 +64,12 @@ namespace Wox.Infrastructure.Storage try { - var t = (T) binaryFormatter.Deserialize(stream); + var t = (T)binaryFormatter.Deserialize(stream); return t; } catch (System.Exception e) { - Log.Error($"Deserialize error for cache file: {FilePath}"); - Log.Exception(e); + Log.Exception($"|BinaryStorage.Deserialize|Deserialize error for file <{FilePath}>", e); return defaultData; } finally @@ -110,8 +109,7 @@ namespace Wox.Infrastructure.Storage } catch (SerializationException e) { - Log.Error($"Serialize error for cache file: {FilePath}"); - Log.Exception(e); + Log.Exception($"|BinaryStorage.Save|serialize error for file <{FilePath}>", e); } } } diff --git a/Wox.Infrastructure/Storage/JsonStorage.cs b/Wox.Infrastructure/Storage/JsonStorage.cs index 9044c35686..045c7fa697 100644 --- a/Wox.Infrastructure/Storage/JsonStorage.cs +++ b/Wox.Infrastructure/Storage/JsonStorage.cs @@ -60,7 +60,7 @@ namespace Wox.Infrastructure.Storage catch (JsonSerializationException e) { LoadDefault(); - Log.Exception(e); + Log.Exception($"|JsonStrorage.Deserialize|Deserialize error for json <{FilePath}>", e); } } diff --git a/Wox/App.xaml.cs b/Wox/App.xaml.cs index 632db9c0d6..12d07f2556 100644 --- a/Wox/App.xaml.cs +++ b/Wox/App.xaml.cs @@ -37,9 +37,9 @@ namespace Wox private void OnStartup(object sender, StartupEventArgs e) { - Stopwatch.Normal("Startup Time", () => + Stopwatch.Normal("|App.OnStartup|Startup cost", () => { - Log.Info("-------------------------- Begin Wox startup --------------------------"); + Log.Info("|App.OnStartup|Begin Wox startup ----------------------------------------------------"); RegisterDispatcherUnhandledException(); var settingVM = new SettingWindowViewModel(); @@ -62,7 +62,7 @@ namespace Wox AutoUpdates(); mainVM.MainWindowVisibility = _settings.HideOnStartup ? Visibility.Hidden : Visibility.Visible; - Log.Info("-------------------------- End Wox startup --------------------------"); + Log.Info("|App.OnStartup|End Wox startup ---------------------------------------------------- "); }); } @@ -121,8 +121,7 @@ namespace Wox AppDomain.CurrentDomain.UnhandledException += ErrorReporting.UnhandledExceptionHandle; AppDomain.CurrentDomain.FirstChanceException += (s, e) => { - Log.Error("First Chance Exception:"); - Log.Exception(e.Exception); + Log.Exception("|App.RegisterAppDomainExceptions|First Chance Exception:", e.Exception); }; } diff --git a/Wox/Helper/ErrorReporting.cs b/Wox/Helper/ErrorReporting.cs index 56153af748..84aff62eb6 100644 --- a/Wox/Helper/ErrorReporting.cs +++ b/Wox/Helper/ErrorReporting.cs @@ -1,8 +1,8 @@ using System; using System.Windows; using System.Windows.Threading; +using NLog; using Wox.Infrastructure.Exception; -using Wox.Infrastructure.Logger; namespace Wox.Helper { @@ -10,7 +10,8 @@ namespace Wox.Helper { public static void Report(Exception e) { - Log.Fatal(e); + var logger = LogManager.GetLogger("UnHandledException"); + logger.Fatal(ExceptionFormatter.FormatExcpetion(e)); new CrashReporter(e).Show(); } diff --git a/Wox/ViewModel/ResultViewModel.cs b/Wox/ViewModel/ResultViewModel.cs index aa9b597adc..a2ce53de2b 100644 --- a/Wox/ViewModel/ResultViewModel.cs +++ b/Wox/ViewModel/ResultViewModel.cs @@ -30,7 +30,7 @@ namespace Wox.ViewModel } catch (Exception e) { - Log.Exception(e); + Log.Exception($"|ResultViewModel.Image|IcoPath is empty and exception when calling Icon() for result <{Result.Title}> of plugin <{Result.PluginDirectory}>", e); return ImageLoader.Load(Result.IcoPath); } } diff --git a/Wox/Wox.csproj b/Wox/Wox.csproj index 3749bfc1e1..748c11a5cd 100644 --- a/Wox/Wox.csproj +++ b/Wox/Wox.csproj @@ -113,6 +113,10 @@ ..\packages\NHotkey.Wpf.1.2.1\lib\net35\NHotkey.Wpf.dll True + + ..\packages\NLog.4.2.0\lib\net45\NLog.dll + True + ..\packages\squirrel.windows.1.4.0\lib\Net45\NuGet.Squirrel.dll True @@ -307,7 +311,9 @@ Designer MSBuild:Compile - + + Designer + MSBuild:Compile Designer diff --git a/Wox/packages.config b/Wox/packages.config index 9424c05fe1..e22ffc4a85 100644 --- a/Wox/packages.config +++ b/Wox/packages.config @@ -8,6 +8,7 @@ +