Improve logging for PT Run (#6800)

* init code pass

* adjusting tabbing for readabilty

* few small adjustments
This commit is contained in:
Clint Rutkas
2020-09-23 16:32:06 -07:00
committed by GitHub
parent dafc1e0c7d
commit b071220b6c
35 changed files with 186 additions and 289 deletions

View File

@@ -22,51 +22,29 @@ namespace Microsoft.Plugin.Program.Logger
/// Logs an exception
/// </summary>
[MethodImpl(MethodImplOptions.Synchronized)]
internal static void LogException(string classname, string callingMethodName, string loadingProgramPath, string interpretationMessage, Exception e)
internal static void Exception(string message, Exception ex, Type fullClassName, string loadingProgramPath, [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
Debug.WriteLine($"ERROR{classname}|{callingMethodName}|{loadingProgramPath}|{interpretationMessage}");
// internal static void LogException(string classname, string callingMethodName, string loadingProgramPath, string interpretationMessage, Exception e)
var possibleResolution = "Not yet known";
var errorStatus = "UNKNOWN";
if (IsKnownWinProgramError(e, callingMethodName) || IsKnownUWPProgramError(e, callingMethodName))
if (IsKnownWinProgramError(ex, methodName) || IsKnownUWPProgramError(ex, methodName))
{
possibleResolution = "Can be ignored and Wox should still continue, however the program may not be loaded";
errorStatus = "KNOWN";
}
var calledMethod = e.TargetSite != null ? e.TargetSite.ToString() : e.StackTrace;
var calledMethod = ex.TargetSite != null ? ex.TargetSite.ToString() : ex.StackTrace;
calledMethod = string.IsNullOrEmpty(calledMethod) ? "Not available" : calledMethod;
var msg = $"Error status: {errorStatus}"
+ $"\nProgram path: {loadingProgramPath}"
+ $"\nException thrown in called method: {calledMethod}"
+ $"\nPossible interpretation of the error: {interpretationMessage}"
+ $"\nPossible resolution: {possibleResolution}";
var msg = $"\tError status: {errorStatus}"
+ $"\n\t\tProgram path: {loadingProgramPath}"
+ $"\n\t\tException thrown in called method: {calledMethod}"
+ $"\n\t\tPossible interpretation of the error: {message}"
+ $"\n\t\tPossible resolution: {possibleResolution}";
// removed looping logic since that is inside Log class
Log.Exception(classname, msg, e, callingMethodName);
}
/// <summary>
/// Please follow exception format: |class name|calling method name|loading program path|user friendly message that explains the error
/// => Example: |Win32|LnkProgram|c:\..\chrome.exe|Permission denied on directory, but Wox should continue
/// </summary>
[MethodImpl(MethodImplOptions.Synchronized)]
internal static void LogException(string message, Exception e)
{
var parts = message.Split('|');
if (parts.Length < 4)
{
Log.Exception($"|ProgramLogger|LogException|Fail to log exception in program logger, parts length is too small: {parts.Length}, message: {message}", e);
}
var classname = parts[1];
var callingMethodName = parts[2];
var loadingProgramPath = parts[3];
var interpretationMessage = parts[4];
LogException(classname, callingMethodName, loadingProgramPath, interpretationMessage, e);
Log.Exception(msg, ex, fullClassName, methodName, sourceFilePath, sourceLineNumber);
}
private static bool IsKnownWinProgramError(Exception e, string callingMethodName)

View File

@@ -4,6 +4,7 @@
using System;
using System.IO;
using System.Reflection;
using Microsoft.Plugin.Program.Logger;
using Package = Windows.ApplicationModel.Package;
@@ -51,7 +52,7 @@ namespace Microsoft.Plugin.Program.Programs
}
catch (Exception e) when (e is ArgumentException || e is FileNotFoundException || e is DirectoryNotFoundException)
{
ProgramLogger.LogException($"PackageWrapper", "GetWrapperFromPackage", "Path could not be determined", $"Exception {package.Id.Name}", e);
ProgramLogger.Exception($"Exception {package.Id.Name}", e, MethodBase.GetCurrentMethod().DeclaringType, "Path could not be determined");
return new PackageWrapper(
package.Id.Name,
package.Id.FullName,

View File

@@ -38,7 +38,7 @@ namespace Microsoft.Plugin.Program.Programs
}
catch (Exception e)
{
Log.Error(nameof(PackageManagerWrapper), e.Message, nameof(FindPackagesForCurrentUser));
Log.Error(e.Message, GetType());
}
}
}

View File

@@ -142,7 +142,7 @@ namespace Microsoft.Plugin.Program.Programs
}
catch (System.IO.FileNotFoundException ex)
{
ProgramLogger.LogException($"|Win32| ShellLinkHelper.retrieveTargetPath | {path} | Path could not be retrieved", ex);
ProgramLogger.Exception("Path could not be retrieved", ex, GetType(), path);
return string.Empty;
}

View File

@@ -6,6 +6,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Xml.Linq;
@@ -80,9 +81,7 @@ namespace Microsoft.Plugin.Program.Programs
else
{
var e = Marshal.GetExceptionForHR((int)hResult);
ProgramLogger.LogException(
$"|UWP|InitializeAppInfo|{path}" +
"|Error caused while trying to get the details of the UWP program", e);
ProgramLogger.Exception("Error caused while trying to get the details of the UWP program", e, GetType(), path);
Apps = new List<UWPApplication>().ToArray();
}
@@ -104,7 +103,7 @@ namespace Microsoft.Plugin.Program.Programs
}
else
{
Log.Error($"|UWP.XmlNamespaces|Error occurred while trying to get the XML from {path}");
Log.Error($"Error occurred while trying to get the XML from {path}", MethodBase.GetCurrentMethod().DeclaringType);
return Array.Empty<string>();
}
@@ -128,10 +127,7 @@ namespace Microsoft.Plugin.Program.Programs
}
}
ProgramLogger.LogException(
$"|UWP|XmlNamespaces|{Location}" +
"|Trying to get the package version of the UWP program, but a unknown UWP appmanifest version "
+ $"{FullName} from location {Location} is returned.", new FormatException());
ProgramLogger.Exception($"|Trying to get the package version of the UWP program, but a unknown UWP appmanifest version {FullName} from location {Location} is returned.", new FormatException(), GetType(), Location);
Version = PackageVersion.Unknown;
}
@@ -153,9 +149,8 @@ namespace Microsoft.Plugin.Program.Programs
}
catch (Exception e)
{
ProgramLogger.LogException(
$"|UWP|All|{p.InstalledLocation}|An unexpected error occurred and "
+ $"unable to convert Package to UWP for {p.FullName}", e);
ProgramLogger.Exception($"Unable to convert Package to UWP for {p.FullName}", e, MethodBase.GetCurrentMethod().DeclaringType, p.InstalledLocation);
return Array.Empty<UWPApplication>();
}
@@ -190,7 +185,7 @@ namespace Microsoft.Plugin.Program.Programs
}
catch (Exception e)
{
ProgramLogger.LogException("UWP", "CurrentUserPackages", $"id", "An unexpected error occurred and unable to verify if package is valid", e);
ProgramLogger.Exception("An unexpected error occurred and unable to verify if package is valid", e, MethodBase.GetCurrentMethod().DeclaringType, "id");
return false;
}

View File

@@ -185,7 +185,7 @@ namespace Microsoft.Plugin.Program.Programs
}
catch (Exception e)
{
Log.Exception($"|Microsoft.Plugin.Program.UWP.ContextMenu| Failed to open {Name} in console, {e.Message}", e);
Log.Exception($"Failed to open {Name} in console, {e.Message}", e, GetType());
return false;
}
},
@@ -310,9 +310,8 @@ namespace Microsoft.Plugin.Program.Programs
}
else
{
ProgramLogger.LogException(
$"|UWP|ResourceFromPri|{Package.Location}|Can't load null or empty result "
+ $"pri {source} in uwp location {Package.Location}", new NullReferenceException());
ProgramLogger.Exception($"Can't load null or empty result pri {source} in uwp location {Package.Location}", new NullReferenceException(), GetType(), Package.Location);
return string.Empty;
}
}
@@ -325,7 +324,8 @@ namespace Microsoft.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);
ProgramLogger.LogException($"|UWP|ResourceFromPri|{Package.Location}|Load pri failed {source} with HResult {hResult} and location {Package.Location}", e);
ProgramLogger.Exception($"Load pri failed {source} with HResult {hResult} and location {Package.Location}", e, GetType(), Package.Location);
return string.Empty;
}
}
@@ -569,9 +569,7 @@ namespace Microsoft.Plugin.Program.Programs
{
LogoPath = string.Empty;
LogoType = LogoType.Error;
ProgramLogger.LogException(
$"|UWP|LogoPathFromUri|{Package.Location}" +
$"|{UserModelId} can't find logo uri for {uri} in package location: {Package.Location}", new FileNotFoundException());
ProgramLogger.Exception($"|{UserModelId} can't find logo uri for {uri} in package location: {Package.Location}", new FileNotFoundException(), GetType(), Package.Location);
}
}
@@ -589,6 +587,8 @@ namespace Microsoft.Plugin.Program.Programs
}
}
private const int _dpiScale100 = 96;
private ImageSource PlatedImage(BitmapImage image)
{
if (!string.IsNullOrEmpty(BackgroundColor))
@@ -630,21 +630,21 @@ namespace Microsoft.Plugin.Program.Programs
var context = visual.RenderOpen();
context.DrawDrawing(group);
context.Close();
const int dpiScale100 = 96;
var bitmap = new RenderTargetBitmap(
Convert.ToInt32(width),
Convert.ToInt32(height),
dpiScale100,
dpiScale100,
_dpiScale100,
_dpiScale100,
PixelFormats.Pbgra32);
bitmap.Render(visual);
return bitmap;
}
else
{
ProgramLogger.LogException(
$"|UWP|PlatedImage|{Package.Location}|Unable to convert background string {BackgroundColor} " +
$"to color for {Package.Location}", new InvalidOperationException());
ProgramLogger.Exception($"Unable to convert background string {BackgroundColor} to color for {Package.Location}", new InvalidOperationException(), GetType(), Package.Location);
return new BitmapImage(new Uri(Constant.ErrorIcon));
}
@@ -674,9 +674,7 @@ namespace Microsoft.Plugin.Program.Programs
}
else
{
ProgramLogger.LogException(
$"|UWP|ImageFromPath|{path}|Unable to get logo for {UserModelId} from {path} and" +
$" located in {Package.Location}", new FileNotFoundException());
ProgramLogger.Exception($"Unable to get logo for {UserModelId} from {path} and located in {Package.Location}", new FileNotFoundException(), GetType(), path);
return new BitmapImage(new Uri(ImageLoader.ErrorIconPath));
}
}

View File

@@ -316,7 +316,7 @@ namespace Microsoft.Plugin.Program.Programs
}
catch (Exception e)
{
Log.Exception($"|Microsoft.Plugin.Program.Win32.ContextMenu| Failed to open {Name} in console, {e.Message}", e);
Log.Exception($"|Failed to open {Name} in console, {e.Message}", e, GetType());
return false;
}
},
@@ -351,9 +351,7 @@ namespace Microsoft.Plugin.Program.Programs
}
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
{
ProgramLogger.LogException(
$"|Win32|Win32Program|{path}" +
$"|Permission denied when trying to load the program from {path}", e);
ProgramLogger.Exception($"|Permission denied when trying to load the program from {path}", e, MethodBase.GetCurrentMethod().DeclaringType, path);
return new Win32Program() { Valid = false, Enabled = false };
}
@@ -386,7 +384,7 @@ namespace Microsoft.Plugin.Program.Programs
{
// To catch the exception if the uri cannot be parsed.
// Link to watson crash: https://watsonportal.microsoft.com/Failure?FailureSearchText=5f871ea7-e886-911f-1b31-131f63f6655b
ProgramLogger.LogException($"|Win32Program|InternetShortcutProgram|{urlPath}|url could not be parsed", e);
ProgramLogger.Exception($"url could not be parsed", e, MethodBase.GetCurrentMethod().DeclaringType, urlPath);
return new Win32Program() { Valid = false, Enabled = false };
}
@@ -426,9 +424,7 @@ namespace Microsoft.Plugin.Program.Programs
}
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
{
ProgramLogger.LogException(
$"|Win32|InternetShortcutProgram|{path}" +
$"|Permission denied when trying to load the program from {path}", e);
ProgramLogger.Exception($"|Permission denied when trying to load the program from {path}", e, MethodBase.GetCurrentMethod().DeclaringType, path);
return new Win32Program() { Valid = false, Enabled = false };
}
@@ -476,9 +472,7 @@ namespace Microsoft.Plugin.Program.Programs
// Error caused likely due to trying to get the description of the program
catch (Exception e)
{
ProgramLogger.LogException(
$"|Win32|LnkProgram|{path}" +
"|An unexpected error occurred in the calling method LnkProgram", e);
ProgramLogger.Exception("An unexpected error occurred in the calling method LnkProgram", e, MethodBase.GetCurrentMethod().DeclaringType, path);
program.Valid = false;
return program;
@@ -501,17 +495,13 @@ namespace Microsoft.Plugin.Program.Programs
}
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
{
ProgramLogger.LogException(
$"|Win32|ExeProgram|{path}" +
$"|Permission denied when trying to load the program from {path}", e);
ProgramLogger.Exception($"|Permission denied when trying to load the program from {path}", e, MethodBase.GetCurrentMethod().DeclaringType, path);
return new Win32Program() { Valid = false, Enabled = false };
}
catch (FileNotFoundException e)
{
ProgramLogger.LogException(
$"|Win32|ExeProgram|{path}" +
$"|Unable to locate exe file at {path}", e);
ProgramLogger.Exception($"|Unable to locate exe file at {path}", e, MethodBase.GetCurrentMethod().DeclaringType, path);
return new Win32Program() { Valid = false, Enabled = false };
}
@@ -619,17 +609,13 @@ namespace Microsoft.Plugin.Program.Programs
}
catch (DirectoryNotFoundException e)
{
ProgramLogger.LogException(
$"|Win32|ProgramPaths|{currentDirectory}" +
"|The directory trying to load the program from does not exist", e);
ProgramLogger.Exception("|The directory trying to load the program from does not exist", e, MethodBase.GetCurrentMethod().DeclaringType, currentDirectory);
}
}
}
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
{
ProgramLogger.LogException(
$"|Win32|ProgramPaths|{currentDirectory}" +
$"|Permission denied when trying to load programs from {currentDirectory}", e);
ProgramLogger.Exception($"|Permission denied when trying to load programs from {currentDirectory}", e, MethodBase.GetCurrentMethod().DeclaringType, currentDirectory);
}
try
@@ -647,9 +633,7 @@ namespace Microsoft.Plugin.Program.Programs
}
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
{
ProgramLogger.LogException(
$"|Win32|ProgramPaths|{currentDirectory}" +
$"|Permission denied when trying to load programs from {currentDirectory}", e);
ProgramLogger.Exception($"|Permission denied when trying to load programs from {currentDirectory}", e, MethodBase.GetCurrentMethod().DeclaringType, currentDirectory);
}
}
while (folderQueue.Any());
@@ -841,9 +825,7 @@ namespace Microsoft.Plugin.Program.Programs
}
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
{
ProgramLogger.LogException(
$"|Win32|GetProgramPathFromRegistrySubKeys|{path}" +
$"|Permission denied when trying to load the program from {path}", e);
ProgramLogger.Exception($"|Permission denied when trying to load the program from {path}", e, MethodBase.GetCurrentMethod().DeclaringType, path);
return string.Empty;
}
@@ -963,7 +945,7 @@ namespace Microsoft.Plugin.Program.Programs
}
catch (Exception e)
{
ProgramLogger.LogException("|Win32|All|Not available|An unexpected error occurred", e);
ProgramLogger.Exception("An unexpected error occurred", e, MethodBase.GetCurrentMethod().DeclaringType, "Not available");
return Array.Empty<Win32Program>();
}

View File

@@ -53,7 +53,7 @@ namespace Microsoft.Plugin.Program.Storage
// eg. "Could not find file 'C:\\Program Files\\WindowsApps\\Microsoft.WindowsTerminalPreview_2020.616.45.0_neutral_~_8wekyb3d8bbwe\\AppxManifest.xml'."
catch (System.IO.FileNotFoundException e)
{
ProgramLogger.LogException($"|UWP|OnPackageInstalling|{args.Package.InstalledLocation}|{e.Message}", e);
ProgramLogger.Exception(e.Message, e, GetType(), args.Package.InstalledLocation.ToString());
}
}
}
@@ -66,6 +66,7 @@ namespace Microsoft.Plugin.Program.Storage
var packageWrapper = PackageWrapper.GetWrapperFromPackage(args.Package);
var uwp = new UWP(packageWrapper);
var apps = Items.Where(a => a.Package.Equals(uwp)).ToArray();
foreach (var app in apps)
{
Remove(app);

View File

@@ -94,7 +94,7 @@ namespace Microsoft.Plugin.Program.Storage
}
catch (Exception ex)
{
Log.Info($"|Win32ProgramRepository|OnAppRenamed-{extension}Program|{oldPath}|Unable to create program from {oldPath}| {ex.Message}");
Log.Exception($"OnAppRenamed-{extension}Program|{oldPath}|Unable to create program from {oldPath}", ex, GetType());
}
// To remove the old app which has been renamed and to add the new application.
@@ -134,7 +134,7 @@ namespace Microsoft.Plugin.Program.Storage
}
catch (Exception ex)
{
Log.Info($"|Win32ProgramRepository|OnAppDeleted-{extension}Program|{path}|Unable to create program from {path}| {ex.Message}");
Log.Exception($"OnAppDeleted-{extension}Program|{path}|Unable to create program from {path}", ex, GetType());
}
if (app != null)