rerouting to Log class vs custom logic (#6688)

This commit is contained in:
Clint Rutkas 2020-09-18 12:58:52 -07:00 committed by GitHub
parent ce835418cb
commit 94d8b4a122
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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
/// </summary>
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;
}
/// <summary>
/// Logs an exception
/// </summary>
@ -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);
}
/// <summary>
@ -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];