mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 19:57:07 +02:00
Better logger
1. Throw exception for fatal/error log when debugging 2. Write to debug output for warn/debug/info log when debugging 3. part of #355
This commit is contained in:
@@ -1,163 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace Wox.Core.Exception
|
||||
{
|
||||
public class ExceptionFormatter
|
||||
{
|
||||
public static string FormatExcpetion(System.Exception exception)
|
||||
{
|
||||
return CreateExceptionReport(exception);
|
||||
}
|
||||
|
||||
private static string CreateExceptionReport(System.Exception ex)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine();
|
||||
sb.AppendLine("## Exception");
|
||||
sb.AppendLine();
|
||||
sb.AppendLine("```");
|
||||
|
||||
var exlist = new List<StringBuilder>();
|
||||
|
||||
while (ex != null)
|
||||
{
|
||||
var exsb = new StringBuilder();
|
||||
exsb.Append(ex.GetType().FullName);
|
||||
exsb.Append(": ");
|
||||
exsb.AppendLine(ex.Message);
|
||||
if (ex.Source != null)
|
||||
{
|
||||
exsb.Append(" Source: ");
|
||||
exsb.AppendLine(ex.Source);
|
||||
}
|
||||
if (ex.TargetSite != null)
|
||||
{
|
||||
exsb.Append(" TargetAssembly: ");
|
||||
exsb.AppendLine(ex.TargetSite.Module.Assembly.ToString());
|
||||
exsb.Append(" TargetModule: ");
|
||||
exsb.AppendLine(ex.TargetSite.Module.ToString());
|
||||
exsb.Append(" TargetSite: ");
|
||||
exsb.AppendLine(ex.TargetSite.ToString());
|
||||
}
|
||||
exsb.AppendLine(ex.StackTrace);
|
||||
exlist.Add(exsb);
|
||||
|
||||
ex = ex.InnerException;
|
||||
}
|
||||
|
||||
foreach (var result in exlist.Select(o => o.ToString()).Reverse())
|
||||
{
|
||||
sb.AppendLine(result);
|
||||
}
|
||||
sb.AppendLine("```");
|
||||
sb.AppendLine();
|
||||
|
||||
sb.AppendLine("## Environment");
|
||||
sb.AppendLine();
|
||||
sb.Append("* Command Line: ");
|
||||
sb.AppendLine(Environment.CommandLine);
|
||||
sb.Append("* Timestamp: ");
|
||||
sb.AppendLine(XmlConvert.ToString(DateTime.Now));
|
||||
sb.Append("* IntPtr Length: ");
|
||||
sb.AppendLine(IntPtr.Size.ToString());
|
||||
sb.Append("* System Version: ");
|
||||
sb.AppendLine(Environment.OSVersion.VersionString);
|
||||
sb.Append("* CLR Version: ");
|
||||
sb.AppendLine(Environment.Version.ToString());
|
||||
sb.AppendLine("* Installed .NET Framework: ");
|
||||
foreach (var result in GetFrameworkVersionFromRegistry())
|
||||
{
|
||||
sb.Append(" * ");
|
||||
sb.AppendLine(result);
|
||||
}
|
||||
|
||||
sb.AppendLine();
|
||||
sb.AppendLine("## Assemblies - " + AppDomain.CurrentDomain.FriendlyName);
|
||||
sb.AppendLine();
|
||||
foreach (var ass in AppDomain.CurrentDomain.GetAssemblies().OrderBy(o => o.GlobalAssemblyCache ? 50 : 0))
|
||||
{
|
||||
sb.Append("* ");
|
||||
sb.Append(ass.FullName);
|
||||
sb.Append(" (");
|
||||
sb.Append(string.IsNullOrEmpty(ass.Location) ? "not supported" : ass.Location);
|
||||
sb.AppendLine(")");
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
// http://msdn.microsoft.com/en-us/library/hh925568%28v=vs.110%29.aspx
|
||||
private static List<string> GetFrameworkVersionFromRegistry()
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = new List<string>();
|
||||
using (RegistryKey ndpKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
|
||||
{
|
||||
foreach (string versionKeyName in ndpKey.GetSubKeyNames())
|
||||
{
|
||||
if (versionKeyName.StartsWith("v"))
|
||||
{
|
||||
RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
|
||||
string name = (string)versionKey.GetValue("Version", "");
|
||||
string sp = versionKey.GetValue("SP", "").ToString();
|
||||
string install = versionKey.GetValue("Install", "").ToString();
|
||||
if (install != "")
|
||||
if (sp != "" && install == "1")
|
||||
result.Add(string.Format("{0} {1} SP{2}", versionKeyName, name, sp));
|
||||
else
|
||||
result.Add(string.Format("{0} {1}", versionKeyName, name));
|
||||
|
||||
if (name != "")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
foreach (string subKeyName in versionKey.GetSubKeyNames())
|
||||
{
|
||||
RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
|
||||
name = (string)subKey.GetValue("Version", "");
|
||||
if (name != "")
|
||||
sp = subKey.GetValue("SP", "").ToString();
|
||||
install = subKey.GetValue("Install", "").ToString();
|
||||
if (install != "")
|
||||
{
|
||||
if (sp != "" && install == "1")
|
||||
result.Add(string.Format("{0} {1} {2} SP{3}", versionKeyName, subKeyName, name, sp));
|
||||
else if (install == "1")
|
||||
result.Add(string.Format("{0} {1} {2}", versionKeyName, subKeyName, name));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
using (RegistryKey ndpKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\"))
|
||||
{
|
||||
int releaseKey = (int)ndpKey.GetValue("Release");
|
||||
{
|
||||
if (releaseKey == 378389)
|
||||
result.Add("v4.5");
|
||||
|
||||
if (releaseKey == 378675)
|
||||
result.Add("v4.5.1 installed with Windows 8.1");
|
||||
|
||||
if (releaseKey == 378758)
|
||||
result.Add("4.5.1 installed on Windows 8, Windows 7 SP1, or Windows Vista SP2");
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
return new List<string>();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
namespace Wox.Core.Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// Represent exceptions that wox can't handle and MUST close running Wox.
|
||||
/// </summary>
|
||||
public class WoxCritialException : WoxException
|
||||
{
|
||||
public WoxCritialException(string msg) : base(msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
namespace Wox.Core.Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// Base Wox Exceptions
|
||||
/// </summary>
|
||||
public class WoxException : System.Exception
|
||||
{
|
||||
public WoxException(string msg)
|
||||
: base(msg)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public WoxException(string msg, System.Exception innerException)
|
||||
: base(msg, innerException)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
namespace Wox.Core.Exception
|
||||
{
|
||||
public class WoxHttpException :WoxException
|
||||
{
|
||||
public WoxHttpException(string msg) : base(msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
namespace Wox.Core.Exception
|
||||
{
|
||||
public class WoxI18nException:WoxException
|
||||
{
|
||||
public WoxI18nException(string msg) : base(msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace Wox.Core.Exception
|
||||
{
|
||||
public class WoxJsonRPCException : WoxException
|
||||
{
|
||||
public WoxJsonRPCException(string msg)
|
||||
: base(msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
namespace Wox.Core.Exception
|
||||
{
|
||||
public class WoxPluginException : WoxException
|
||||
{
|
||||
public string PluginName { get; set; }
|
||||
|
||||
public WoxPluginException(string pluginName,System.Exception e)
|
||||
: base(e.Message,e)
|
||||
{
|
||||
PluginName = pluginName;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Wox.Core.Exception;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Plugin;
|
||||
|
||||
@@ -19,10 +20,10 @@ namespace Wox.Core.Plugin
|
||||
try
|
||||
{
|
||||
Assembly asm = Assembly.Load(AssemblyName.GetAssemblyName(metadata.ExecuteFilePath));
|
||||
List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IPlugin))).ToList();
|
||||
List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IPlugin))).ToList();
|
||||
if (types.Count == 0)
|
||||
{
|
||||
Log.Warn(string.Format("Couldn't load plugin {0}: didn't find the class that implement IPlugin", metadata.Name));
|
||||
Log.Warn($"Couldn't load plugin {metadata.Name}: didn't find the class that implement IPlugin");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -39,12 +40,7 @@ namespace Wox.Core.Plugin
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Error(string.Format("Couldn't load plugin {0}: {1}", metadata.Name, e.Message));
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
#endif
|
||||
Log.Error(new WoxPluginException(metadata.Name, $"Couldn't load plugin", e));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ namespace Wox.Core.Plugin
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Error(e.Message);
|
||||
Log.Error(e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -47,7 +47,7 @@ namespace Wox.Core.Plugin
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Error(ExceptionFormatter.FormatExcpetion(e));
|
||||
Log.Fatal(e);
|
||||
}
|
||||
}
|
||||
PluginMetadata metadata = GetPluginMetadata(directory);
|
||||
@@ -63,7 +63,7 @@ namespace Wox.Core.Plugin
|
||||
string configPath = Path.Combine(pluginDirectory, pluginConfigName);
|
||||
if (!File.Exists(configPath))
|
||||
{
|
||||
Log.Warn(string.Format("parse plugin {0} failed: didn't find config file.", configPath));
|
||||
Log.Warn($"parse plugin {configPath} failed: didn't find config file.");
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -77,40 +77,25 @@ namespace Wox.Core.Plugin
|
||||
// for plugin still use old ActionKeyword
|
||||
metadata.ActionKeyword = metadata.ActionKeywords?[0];
|
||||
}
|
||||
catch (System.Exception)
|
||||
catch (System.Exception e)
|
||||
{
|
||||
string error = string.Format("Parse plugin config {0} failed: json format is not valid", configPath);
|
||||
Log.Warn(error);
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw new WoxException(error);
|
||||
}
|
||||
#endif
|
||||
string msg = $"Parse plugin config {configPath} failed: json format is not valid";
|
||||
Log.Error(new WoxException(msg));
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
if (!AllowedLanguage.IsAllowed(metadata.Language))
|
||||
{
|
||||
string error = string.Format("Parse plugin config {0} failed: invalid language {1}", configPath, metadata.Language);
|
||||
Log.Warn(error);
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw new WoxException(error);
|
||||
}
|
||||
#endif
|
||||
string msg = $"Parse plugin config {configPath} failed: invalid language {metadata.Language}";
|
||||
Log.Error(new WoxException(msg));
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!File.Exists(metadata.ExecuteFilePath))
|
||||
{
|
||||
string error = string.Format("Parse plugin config {0} failed: ExecuteFile {1} didn't exist", configPath, metadata.ExecuteFilePath);
|
||||
Log.Warn(error);
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw new WoxException(error);
|
||||
}
|
||||
#endif
|
||||
string msg = $"Parse plugin config {configPath} failed: ExecuteFile {metadata.ExecuteFilePath} didn't exist";
|
||||
Log.Error(new WoxException(msg));
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ namespace Wox.Core.Plugin
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Error(e.Message);
|
||||
Log.Error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,7 +69,7 @@ namespace Wox.Core.Plugin
|
||||
/// </summary>
|
||||
public static void Init(IPublicAPI api)
|
||||
{
|
||||
if (api == null) throw new WoxCritialException("api is null");
|
||||
if (api == null) throw new WoxFatalException("api is null");
|
||||
|
||||
SetupPluginDirectories();
|
||||
API = api;
|
||||
@@ -164,7 +164,7 @@ namespace Wox.Core.Plugin
|
||||
if (customizedPluginConfig != null && customizedPluginConfig.Disabled) continue;
|
||||
if (IsInstantQueryPlugin(plugin))
|
||||
{
|
||||
Stopwatch.Debug($"Instant Query for {plugin.Metadata.Name}", () =>
|
||||
Stopwatch.Normal($"Instant QueryForPlugin for {plugin.Metadata.Name}", () =>
|
||||
{
|
||||
QueryForPlugin(plugin, query);
|
||||
});
|
||||
@@ -173,7 +173,10 @@ namespace Wox.Core.Plugin
|
||||
{
|
||||
ThreadPool.QueueUserWorkItem(state =>
|
||||
{
|
||||
QueryForPlugin(plugin, query);
|
||||
Stopwatch.Normal($"Normal QueryForPlugin for {plugin.Metadata.Name}", () =>
|
||||
{
|
||||
QueryForPlugin(plugin, query);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -184,7 +187,7 @@ namespace Wox.Core.Plugin
|
||||
try
|
||||
{
|
||||
List<Result> results = new List<Result>();
|
||||
var milliseconds = Stopwatch.Normal($"Query for {pair.Metadata.Name}", () =>
|
||||
var milliseconds = Stopwatch.Normal($"Plugin.Query cost for {pair.Metadata.Name}", () =>
|
||||
{
|
||||
results = pair.Plugin.Query(query) ?? results;
|
||||
results.ForEach(o => { o.PluginID = pair.Metadata.ID; });
|
||||
@@ -195,7 +198,7 @@ namespace Wox.Core.Plugin
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
throw new WoxPluginException(pair.Metadata.Name, e);
|
||||
throw new WoxPluginException(pair.Metadata.Name, $"QueryForPlugin failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,12 +242,7 @@ namespace Wox.Core.Plugin
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Error($"Couldn't load plugin context menus {pluginPair.Metadata.Name}: {e.Message}");
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
#endif
|
||||
Log.Error(new WoxPluginException(pluginPair.Metadata.Name, $"Couldn't load plugin context menus", e));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ namespace Wox.Core.Theme
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Error(e.Message);
|
||||
Log.Error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,13 +57,6 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="APIServer.cs" />
|
||||
<Compile Include="Exception\ExceptionFormatter.cs" />
|
||||
<Compile Include="Exception\WoxCritialException.cs" />
|
||||
<Compile Include="Exception\WoxException.cs" />
|
||||
<Compile Include="Exception\WoxHttpException.cs" />
|
||||
<Compile Include="Exception\WoxI18nException.cs" />
|
||||
<Compile Include="Exception\WoxJsonRPCException.cs" />
|
||||
<Compile Include="Exception\WoxPluginException.cs" />
|
||||
<Compile Include="Updater\Release.cs" />
|
||||
<Compile Include="Updater\UpdaterManager.cs" />
|
||||
<Compile Include="Updater\WoxUpdateSource.cs" />
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace Wox.Core.i18n
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Error(e.Message);
|
||||
Log.Error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -122,12 +122,8 @@ namespace Wox.Core.i18n
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Warn("Update Plugin metadata translation failed:" + e.Message);
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
#endif
|
||||
var woxPluginException = new WoxPluginException(pluginPair.Metadata.Name, "Update Plugin metadata translation failed:", e);
|
||||
Log.Error(woxPluginException);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user