From 94d8b4a12276beea258e9fbee654a270c9cdefb0 Mon Sep 17 00:00:00 2001 From: Clint Rutkas Date: Fri, 18 Sep 2020 12:58:52 -0700 Subject: [PATCH] rerouting to Log class vs custom logic (#6688) --- .../Logger/ProgramLogger.cs | 82 ++++--------------- 1 file changed, 16 insertions(+), 66 deletions(-) diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Logger/ProgramLogger.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Logger/ProgramLogger.cs index 74de962f93..ed8e4097f2 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Logger/ProgramLogger.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Logger/ProgramLogger.cs @@ -7,10 +7,7 @@ using System.Diagnostics; using System.IO; using System.Runtime.CompilerServices; using System.Security; -using NLog; -using NLog.Config; -using NLog.Targets; -using Wox.Infrastructure; +using Wox.Infrastructure.Logger; namespace Microsoft.Plugin.Program.Logger { @@ -21,32 +18,6 @@ namespace Microsoft.Plugin.Program.Logger /// internal static class ProgramLogger { - public const string DirectoryName = "Logs"; - - static ProgramLogger() - { - var path = Path.Combine(Constant.DataDirectory, DirectoryName, Constant.Version); - if (!Directory.Exists(path)) - { - Directory.CreateDirectory(path); - } - - var configuration = new LoggingConfiguration(); - using (var target = new FileTarget()) - { - configuration.AddTarget("file", target); - target.FileName = path.Replace(@"\", "/", StringComparison.Ordinal) + "/${shortdate}.txt"; -#if DEBUG - var rule = new LoggingRule("*", LogLevel.Debug, target); -#else - var rule = new LoggingRule("*", LogLevel.Error, target); -#endif - configuration.LoggingRules.Add(rule); - } - - LogManager.Configuration = configuration; - } - /// /// Logs an exception /// @@ -55,45 +26,26 @@ namespace Microsoft.Plugin.Program.Logger { Debug.WriteLine($"ERROR{classname}|{callingMethodName}|{loadingProgramPath}|{interpretationMessage}"); - var logger = LogManager.GetLogger(string.Empty); - - var innerExceptionNumber = 1; - var possibleResolution = "Not yet known"; var errorStatus = "UNKNOWN"; - logger.Error("------------- BEGIN Microsoft.Plugin.Program exception -------------"); - - do + if (IsKnownWinProgramError(e, callingMethodName) || IsKnownUWPProgramError(e, callingMethodName)) { - if (IsKnownWinProgramError(e, callingMethodName) || IsKnownUWPProgramError(e, callingMethodName)) - { - 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; - - calledMethod = string.IsNullOrEmpty(calledMethod) ? "Not available" : calledMethod; - - logger.Error($"\nException full name: {e.GetType().FullName}" - + $"\nError status: {errorStatus}" - + $"\nClass name: {classname}" - + $"\nCalling method: {callingMethodName}" - + $"\nProgram path: {loadingProgramPath}" - + $"\nInnerException number: {innerExceptionNumber}" - + $"\nException message: {e.Message}" - + $"\nException error type: HResult {e.HResult}" - + $"\nException thrown in called method: {calledMethod}" - + $"\nPossible interpretation of the error: {interpretationMessage}" - + $"\nPossible resolution: {possibleResolution}"); - - innerExceptionNumber++; - e = e.InnerException; + possibleResolution = "Can be ignored and Wox should still continue, however the program may not be loaded"; + errorStatus = "KNOWN"; } - while (e != null); - logger.Error("------------- END Microsoft.Plugin.Program exception -------------"); + var calledMethod = e.TargetSite != null ? e.TargetSite.ToString() : e.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}"; + + // removed looping logic since that is inside Log class + Log.Exception(classname, msg, e, callingMethodName); } /// @@ -103,12 +55,10 @@ namespace Microsoft.Plugin.Program.Logger [MethodImpl(MethodImplOptions.Synchronized)] internal static void LogException(string message, Exception e) { - // Index 0 is always empty. var parts = message.Split('|'); if (parts.Length < 4) { - var logger = LogManager.GetLogger(string.Empty); - logger.Error(e, $"fail to log exception in program logger, parts length is too small: {parts.Length}, message: {message}"); + 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];