mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 03:07:04 +02:00
Merge branch 'master' into dotnet45
This commit is contained in:
163
Wox.Infrastructure/Exception/ExceptionFormatter.cs
Normal file
163
Wox.Infrastructure/Exception/ExceptionFormatter.cs
Normal file
@@ -0,0 +1,163 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace Wox.Infrastructure.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>();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
19
Wox.Infrastructure/Exception/WoxException.cs
Normal file
19
Wox.Infrastructure/Exception/WoxException.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
namespace Wox.Infrastructure.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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Wox.Infrastructure/Exception/WoxFatalException.cs
Normal file
12
Wox.Infrastructure/Exception/WoxFatalException.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace Wox.Infrastructure.Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// Represent exceptions that wox can't handle and MUST close running Wox.
|
||||
/// </summary>
|
||||
public class WoxFatalException : WoxException
|
||||
{
|
||||
public WoxFatalException(string msg) : base(msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Wox.Infrastructure/Exception/WoxHttpException.cs
Normal file
9
Wox.Infrastructure/Exception/WoxHttpException.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Wox.Infrastructure.Exception
|
||||
{
|
||||
public class WoxHttpException :WoxException
|
||||
{
|
||||
public WoxHttpException(string msg) : base(msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Wox.Infrastructure/Exception/WoxI18nException.cs
Normal file
9
Wox.Infrastructure/Exception/WoxI18nException.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Wox.Infrastructure.Exception
|
||||
{
|
||||
public class WoxI18nException : WoxException
|
||||
{
|
||||
public WoxI18nException(string msg) : base(msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
10
Wox.Infrastructure/Exception/WoxJsonRPCException.cs
Normal file
10
Wox.Infrastructure/Exception/WoxJsonRPCException.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace Wox.Infrastructure.Exception
|
||||
{
|
||||
public class WoxJsonRPCException : WoxException
|
||||
{
|
||||
public WoxJsonRPCException(string msg)
|
||||
: base(msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Wox.Infrastructure/Exception/WoxPluginException.cs
Normal file
20
Wox.Infrastructure/Exception/WoxPluginException.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.Infrastructure.Exception
|
||||
{
|
||||
public class WoxPluginException : WoxException
|
||||
{
|
||||
public string PluginName { get; set; }
|
||||
|
||||
public WoxPluginException(string pluginName, string msg, System.Exception e)
|
||||
: base($"{pluginName} : {msg}", e)
|
||||
{
|
||||
PluginName = pluginName;
|
||||
}
|
||||
|
||||
public WoxPluginException(string pluginName, string msg) : base(msg)
|
||||
{
|
||||
PluginName = pluginName;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using Wox.Plugin;
|
||||
@@ -55,7 +54,7 @@ namespace Wox.Infrastructure.Http
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Logger.Log.Error(e);
|
||||
return string.Empty;
|
||||
@@ -108,7 +107,7 @@ namespace Wox.Infrastructure.Http
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Logger.Log.Error(e);
|
||||
return string.Empty;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using NLog;
|
||||
using Wox.Infrastructure.Exception;
|
||||
|
||||
namespace Wox.Infrastructure.Logger
|
||||
{
|
||||
@@ -7,34 +8,45 @@ namespace Wox.Infrastructure.Logger
|
||||
{
|
||||
private static NLog.Logger logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public static void Error(string msg)
|
||||
public static void Error(System.Exception e)
|
||||
{
|
||||
logger.Error(msg);
|
||||
}
|
||||
|
||||
public static void Error(Exception e)
|
||||
{
|
||||
logger.Error(e.Message + "\r\n" + e.StackTrace);
|
||||
#if DEBUG
|
||||
throw e;
|
||||
#else
|
||||
while (e.InnerException != null)
|
||||
{
|
||||
logger.Error(e.Message);
|
||||
logger.Error(e.StackTrace);
|
||||
e = e.InnerException;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void Debug(string msg)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"DEBUG: {msg}");
|
||||
logger.Debug(msg);
|
||||
}
|
||||
|
||||
public static void Info(string msg)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"INFO: {msg}");
|
||||
logger.Info(msg);
|
||||
}
|
||||
|
||||
public static void Warn(string msg)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"WARN: {msg}");
|
||||
logger.Warn(msg);
|
||||
}
|
||||
|
||||
public static void Fatal(string msg)
|
||||
public static void Fatal(System.Exception e)
|
||||
{
|
||||
logger.Fatal(msg);
|
||||
#if DEBUG
|
||||
throw e;
|
||||
#else
|
||||
logger.Fatal(ExceptionFormatter.FormatExcpetion(e));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,14 +18,6 @@ namespace Wox.Infrastructure
|
||||
#endif
|
||||
}
|
||||
|
||||
[Conditional("DEBUG")]
|
||||
private static void WriteTimeInfo(string name, long milliseconds)
|
||||
{
|
||||
string info = $"{name} : {milliseconds}ms";
|
||||
System.Diagnostics.Debug.WriteLine(info);
|
||||
Log.Info(info);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This stopwatch will also appear only in Debug mode
|
||||
/// </summary>
|
||||
@@ -36,7 +28,8 @@ namespace Wox.Infrastructure
|
||||
action();
|
||||
stopWatch.Stop();
|
||||
var milliseconds = stopWatch.ElapsedMilliseconds;
|
||||
WriteTimeInfo(name, milliseconds);
|
||||
string info = $"{name} : {milliseconds}ms";
|
||||
Log.Debug(info);
|
||||
return milliseconds;
|
||||
}
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace Wox.Infrastructure.Storage
|
||||
serializedObject = LoadDefault();
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw new Exception("deserialize failed");
|
||||
throw new System.Exception("deserialize failed");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -53,7 +53,7 @@ namespace Wox.Infrastructure.Storage
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
serializedObject = LoadDefault();
|
||||
@@ -101,7 +101,7 @@ namespace Wox.Infrastructure.Storage
|
||||
binaryFormatter.Serialize(fileStream, serializedObject);
|
||||
fileStream.Close();
|
||||
}
|
||||
catch (Exception e)
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
#if (DEBUG)
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace Wox.Infrastructure.Storage
|
||||
{
|
||||
serializedObject = JsonConvert.DeserializeObject<T>(json);
|
||||
}
|
||||
catch (Exception)
|
||||
catch (System.Exception)
|
||||
{
|
||||
serializedObject = LoadDefault();
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ namespace Wox.Infrastructure
|
||||
needsCommandLine = peHeaderReader.OptionalHeader64.Subsystem == 3;
|
||||
}
|
||||
|
||||
catch (Exception)
|
||||
catch (System.Exception)
|
||||
{
|
||||
// Error reading the headers. We will try to run the command the standard way.
|
||||
needsCommandLine = false;
|
||||
@@ -149,7 +149,7 @@ namespace Wox.Infrastructure
|
||||
{
|
||||
global::System.Diagnostics.Process.Start(startInfo);
|
||||
}
|
||||
catch (Exception e)
|
||||
catch (System.Exception e)
|
||||
{
|
||||
if (!startInfo.ErrorDialog)
|
||||
throw e;
|
||||
|
||||
@@ -46,9 +46,17 @@
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="WindowsBase" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Exception\ExceptionFormatter.cs" />
|
||||
<Compile Include="Exception\WoxException.cs" />
|
||||
<Compile Include="Exception\WoxFatalException.cs" />
|
||||
<Compile Include="Exception\WoxHttpException.cs" />
|
||||
<Compile Include="Exception\WoxI18nException.cs" />
|
||||
<Compile Include="Exception\WoxJsonRPCException.cs" />
|
||||
<Compile Include="Exception\WoxPluginException.cs" />
|
||||
<Compile Include="Hotkey\InterceptKeys.cs" />
|
||||
<Compile Include="Hotkey\KeyEvent.cs" />
|
||||
<Compile Include="Logger\Log.cs" />
|
||||
|
||||
Reference in New Issue
Block a user