From e191050046c001bd7a75a213e8a54bea307ce69c Mon Sep 17 00:00:00 2001 From: Christian Rondeau Date: Mon, 13 Nov 2017 15:30:08 -0500 Subject: [PATCH] #53: Add logs to diagnose the issue --- GoToWindow/ExitCodes.cs | 1 + GoToWindow/GoToWindowPluginsContainer.cs | 52 ++++++++++++++++++++---- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/GoToWindow/ExitCodes.cs b/GoToWindow/ExitCodes.cs index 132ff11..44d3167 100644 --- a/GoToWindow/ExitCodes.cs +++ b/GoToWindow/ExitCodes.cs @@ -6,6 +6,7 @@ public enum ExitCodes PluginDirectoryNotFound = 120, NoPluginsFound = 121, ErrorLoadingPlugins = 122, + ErrorLoadingPluginsTypes = 123, UnhandledError = 500 } } \ No newline at end of file diff --git a/GoToWindow/GoToWindowPluginsContainer.cs b/GoToWindow/GoToWindowPluginsContainer.cs index 011c9ce..b356fab 100644 --- a/GoToWindow/GoToWindowPluginsContainer.cs +++ b/GoToWindow/GoToWindowPluginsContainer.cs @@ -48,34 +48,68 @@ public static GoToWindowPluginsContainer LoadPlugins() var pluginsContainer = new GoToWindowPluginsContainer(); container.ComposeParts(pluginsContainer); + DiagnosticCatalogComposition(catalog); + if(pluginsContainer.Plugins == null || !pluginsContainer.Plugins.Any()) - throw new InstanceNotFoundException("No plug-ins found"); + { + HandleError(new Exception("No plugins were composed"), ExitCodes.NoPluginsFound, "No plug-ins found. Check that at least GoToWindow.Plugins.Core.dll can be found in the Plugins directory and restart GoToWindow."); + return null; + } pluginsContainer.Plugins = pluginsContainer.Plugins.OrderBy(plugin => plugin.Sequence).ToList(); return pluginsContainer; } catch (InstanceNotFoundException exc) { - HandleError(exc, ExitCodes.NoPluginsFound, "No plug-ins found. Check that at least GoToWindow.Plugins.Core.dll can be found in the Plugins directory and restart GoToWindow."); + HandleError(exc, ExitCodes.NoPluginsFound, "No plug-ins found. Check that at least GoToWindow.Plugins.Core.dll can be found in the Plugins directory and restart GoToWindow."); + return null; + } + catch(ReflectionTypeLoadException exc) + { + HandleError(exc, ExitCodes.ErrorLoadingPluginsTypes, "An error occured while loading plugin types.", string.Join("; ", exc.LoaderExceptions.Select(e => e.Message))); return null; } - catch (Exception exc) + catch (Exception exc) { HandleError(exc, ExitCodes.ErrorLoadingPlugins, "An error occured while loading plug-ins. Try updating or removing plugins other than GoToWindow.Plugins.Core.dll from the Plugins directory and restart GoToWindow."); return null; } } - private static void HandleError(Exception exc, ExitCodes exitCode, string message) - { - var typeLoadExc = exc as ReflectionTypeLoadException; + private static void DiagnosticCatalogComposition(AggregateCatalog catalog) + { + if (!Log.IsDebugEnabled) return; - if (typeLoadExc != null) - Log.Error(string.Join("; ", typeLoadExc.LoaderExceptions.Select(e => e.Message)), typeLoadExc); + try + { + foreach (var c in catalog.Catalogs) + { + if (c is DirectoryCatalog directoryCatalog) + { + Log.Debug($"Loaded directory catalog from '{directoryCatalog.FullPath}', looking for '{directoryCatalog.SearchPattern}'. Loaded: {string.Join(", ", directoryCatalog.LoadedFiles)}"); + } + else + { + Log.Debug($"Loaded catalog of type {c.GetType().FullName}"); + } + } + } + catch (Exception exc) + { + Log.Warn("Failed generating catalog information", exc); + } + } + + private static void HandleError(Exception exc, ExitCodes exitCode, string message, string additionalInfo = null) + { + if (additionalInfo != null) + Log.Error(additionalInfo, exc); else Log.Error(exc); + + - MessageBox.Show(message, "Startup Error", MessageBoxButton.OK, MessageBoxImage.Error); + MessageBox.Show(message, "Startup Error", MessageBoxButton.OK, MessageBoxImage.Error); Application.Current.Shutdown((int)exitCode); } }