enhance exception logging

This commit is contained in:
bao-qian
2016-11-30 00:30:42 +00:00
parent c56e4557f9
commit b43c6c00bb
2 changed files with 27 additions and 7 deletions

View File

@@ -363,9 +363,8 @@ namespace Wox.Plugin.Program.Programs
// for // for
// Microsoft.MicrosoftOfficeHub_17.7608.23501.0_x64__8wekyb3d8bbwe: ms-resource://Microsoft.MicrosoftOfficeHub/officehubintl/AppManifest_GetOffice_Description // 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 // Microsoft.BingFoodAndDrink_3.0.4.336_x64__8wekyb3d8bbwe: ms-resource:AppDescription
Log.Error($"Load {source} failed, HResult error code: {hResult}. Package location: <{Package.Location}>."); var e = Marshal.GetExceptionForHR((int)hResult);
var exception = Marshal.GetExceptionForHR((int)hResult); Log.Error(e, $"Load {source} failed, HResult error code: {hResult}. Package location: <{Package.Location}>.");
Log.Exception(exception);
return string.Empty; return string.Empty;
} }
} }

View File

@@ -1,5 +1,6 @@
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Runtime.CompilerServices;
using NLog; using NLog;
using NLog.Config; using NLog.Config;
using NLog.Targets; using NLog.Targets;
@@ -22,11 +23,13 @@ namespace Wox.Infrastructure.Logger
var configuration = new LoggingConfiguration(); var configuration = new LoggingConfiguration();
var target = new FileTarget(); var target = new FileTarget();
configuration.AddTarget("file", target); configuration.AddTarget("file", target);
target.FileName = "${specialfolder:folder=ApplicationData}/" + Constant.Wox + "/" + DirectoryName + "/" + Constant.Version + "/${shortdate}.txt"; target.FileName = "${specialfolder:folder=ApplicationData}/" + Constant.Wox + "/" + DirectoryName + "/" +
Constant.Version + "/${shortdate}.txt";
var rule = new LoggingRule("*", LogLevel.Info, target); var rule = new LoggingRule("*", LogLevel.Info, target);
configuration.LoggingRules.Add(rule); configuration.LoggingRules.Add(rule);
LogManager.Configuration = configuration; LogManager.Configuration = configuration;
} }
private static string CallerType() private static string CallerType()
{ {
var stackTrace = new StackTrace(); var stackTrace = new StackTrace();
@@ -45,6 +48,24 @@ namespace Wox.Infrastructure.Logger
logger.Error(msg); logger.Error(msg);
} }
[MethodImpl(MethodImplOptions.Synchronized)]
public static void Error(System.Exception e, string msg)
{
var type = CallerType();
var logger = LogManager.GetLogger(type);
System.Diagnostics.Debug.WriteLine($"ERROR: {msg}");
logger.Error("-------------------------- Begin exception --------------------------");
logger.Error(msg);
do
{
logger.Error($"Exception message:\n <{e.Message}>");
logger.Error($"Exception stack trace:\n<{e.StackTrace}>");
e = e.InnerException;
} while (e != null);
logger.Error("-------------------------- End exception --------------------------");
}
[MethodImpl(MethodImplOptions.Synchronized)]
public static void Exception(System.Exception e) public static void Exception(System.Exception e)
{ {
var type = CallerType(); var type = CallerType();
@@ -52,8 +73,8 @@ namespace Wox.Infrastructure.Logger
do do
{ {
logger.Error(e.Message); logger.Error($"Exception message:\n <{e.Message}>");
logger.Error($"\n{e.StackTrace}"); logger.Error($"Exception stack trace:\n<{e.StackTrace}>");
e = e.InnerException; e = e.InnerException;
} while (e != null); } while (e != null);
} }