rerouting to Log class vs custom logic (#6688)
This commit is contained in:
parent
ce835418cb
commit
94d8b4a122
1 changed files with 16 additions and 66 deletions
|
@ -7,10 +7,7 @@ using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Security;
|
using System.Security;
|
||||||
using NLog;
|
using Wox.Infrastructure.Logger;
|
||||||
using NLog.Config;
|
|
||||||
using NLog.Targets;
|
|
||||||
using Wox.Infrastructure;
|
|
||||||
|
|
||||||
namespace Microsoft.Plugin.Program.Logger
|
namespace Microsoft.Plugin.Program.Logger
|
||||||
{
|
{
|
||||||
|
@ -21,32 +18,6 @@ namespace Microsoft.Plugin.Program.Logger
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal static class ProgramLogger
|
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>
|
/// <summary>
|
||||||
/// Logs an exception
|
/// Logs an exception
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -55,45 +26,26 @@ namespace Microsoft.Plugin.Program.Logger
|
||||||
{
|
{
|
||||||
Debug.WriteLine($"ERROR{classname}|{callingMethodName}|{loadingProgramPath}|{interpretationMessage}");
|
Debug.WriteLine($"ERROR{classname}|{callingMethodName}|{loadingProgramPath}|{interpretationMessage}");
|
||||||
|
|
||||||
var logger = LogManager.GetLogger(string.Empty);
|
|
||||||
|
|
||||||
var innerExceptionNumber = 1;
|
|
||||||
|
|
||||||
var possibleResolution = "Not yet known";
|
var possibleResolution = "Not yet known";
|
||||||
var errorStatus = "UNKNOWN";
|
var errorStatus = "UNKNOWN";
|
||||||
|
|
||||||
logger.Error("------------- BEGIN Microsoft.Plugin.Program exception -------------");
|
if (IsKnownWinProgramError(e, callingMethodName) || IsKnownUWPProgramError(e, callingMethodName))
|
||||||
|
|
||||||
do
|
|
||||||
{
|
{
|
||||||
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";
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
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>
|
/// <summary>
|
||||||
|
@ -103,12 +55,10 @@ namespace Microsoft.Plugin.Program.Logger
|
||||||
[MethodImpl(MethodImplOptions.Synchronized)]
|
[MethodImpl(MethodImplOptions.Synchronized)]
|
||||||
internal static void LogException(string message, Exception e)
|
internal static void LogException(string message, Exception e)
|
||||||
{
|
{
|
||||||
// Index 0 is always empty.
|
|
||||||
var parts = message.Split('|');
|
var parts = message.Split('|');
|
||||||
if (parts.Length < 4)
|
if (parts.Length < 4)
|
||||||
{
|
{
|
||||||
var logger = LogManager.GetLogger(string.Empty);
|
Log.Exception($"|ProgramLogger|LogException|Fail to log exception in program logger, parts length is too small: {parts.Length}, message: {message}", e);
|
||||||
logger.Error(e, $"fail to log exception in program logger, parts length is too small: {parts.Length}, message: {message}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var classname = parts[1];
|
var classname = parts[1];
|
||||||
|
|
Loading…
Reference in a new issue