2016-04-28 05:51:31 +08:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
2016-11-30 08:30:42 +08:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2016-04-28 05:51:31 +08:00
|
|
|
|
using NLog;
|
|
|
|
|
using NLog.Config;
|
|
|
|
|
using NLog.Targets;
|
2016-05-25 08:00:10 +08:00
|
|
|
|
using Wox.Infrastructure.Exception;
|
2013-12-20 19:38:10 +08:00
|
|
|
|
|
2014-09-19 16:57:48 +08:00
|
|
|
|
namespace Wox.Infrastructure.Logger
|
2013-12-20 19:38:10 +08:00
|
|
|
|
{
|
2016-04-28 05:51:31 +08:00
|
|
|
|
public static class Log
|
2013-12-20 19:38:10 +08:00
|
|
|
|
{
|
2016-05-21 04:16:25 +08:00
|
|
|
|
public const string DirectoryName = "Logs";
|
|
|
|
|
|
2016-04-28 05:51:31 +08:00
|
|
|
|
static Log()
|
|
|
|
|
{
|
2016-05-21 04:16:25 +08:00
|
|
|
|
var path = Path.Combine(Constant.DataDirectory, DirectoryName, Constant.Version);
|
2016-04-28 05:51:31 +08:00
|
|
|
|
if (!Directory.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
}
|
2013-12-20 19:38:10 +08:00
|
|
|
|
|
2016-04-28 05:51:31 +08:00
|
|
|
|
var configuration = new LoggingConfiguration();
|
|
|
|
|
var target = new FileTarget();
|
|
|
|
|
configuration.AddTarget("file", target);
|
2016-11-30 08:30:42 +08:00
|
|
|
|
target.FileName = "${specialfolder:folder=ApplicationData}/" + Constant.Wox + "/" + DirectoryName + "/" +
|
|
|
|
|
Constant.Version + "/${shortdate}.txt";
|
2016-04-28 05:51:31 +08:00
|
|
|
|
var rule = new LoggingRule("*", LogLevel.Info, target);
|
|
|
|
|
configuration.LoggingRules.Add(rule);
|
|
|
|
|
LogManager.Configuration = configuration;
|
|
|
|
|
}
|
2016-11-30 08:30:42 +08:00
|
|
|
|
|
2016-11-30 08:39:03 +08:00
|
|
|
|
public static string CallerType()
|
2016-04-28 05:51:31 +08:00
|
|
|
|
{
|
|
|
|
|
var stackTrace = new StackTrace();
|
2016-05-08 02:16:13 +08:00
|
|
|
|
var stackFrames = stackTrace.GetFrames().NonNull();
|
2016-04-28 05:51:31 +08:00
|
|
|
|
var callingFrame = stackFrames[2];
|
|
|
|
|
var method = callingFrame.GetMethod();
|
2016-05-08 02:16:13 +08:00
|
|
|
|
var type = $"{method.DeclaringType.NonNull().FullName}.{method.Name}";
|
2016-04-28 05:51:31 +08:00
|
|
|
|
return type;
|
|
|
|
|
}
|
2016-05-16 00:03:06 +08:00
|
|
|
|
|
|
|
|
|
public static void Error(string msg)
|
2015-01-11 21:52:30 +08:00
|
|
|
|
{
|
2016-04-28 05:51:31 +08:00
|
|
|
|
var type = CallerType();
|
|
|
|
|
var logger = LogManager.GetLogger(type);
|
2016-05-16 00:03:06 +08:00
|
|
|
|
System.Diagnostics.Debug.WriteLine($"ERROR: {msg}");
|
|
|
|
|
logger.Error(msg);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-30 08:30:42 +08:00
|
|
|
|
[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)]
|
2016-05-16 00:03:06 +08:00
|
|
|
|
public static void Exception(System.Exception e)
|
|
|
|
|
{
|
|
|
|
|
var type = CallerType();
|
|
|
|
|
var logger = LogManager.GetLogger(type);
|
|
|
|
|
|
|
|
|
|
do
|
2015-11-11 08:42:49 +08:00
|
|
|
|
{
|
2016-11-30 08:30:42 +08:00
|
|
|
|
logger.Error($"Exception message:\n <{e.Message}>");
|
|
|
|
|
logger.Error($"Exception stack trace:\n<{e.StackTrace}>");
|
2015-11-11 08:42:49 +08:00
|
|
|
|
e = e.InnerException;
|
2016-05-16 00:03:06 +08:00
|
|
|
|
} while (e != null);
|
2015-01-11 21:52:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-30 08:39:03 +08:00
|
|
|
|
public static void Debug(string type, string msg)
|
2014-03-23 17:43:46 +08:00
|
|
|
|
{
|
2016-04-28 05:51:31 +08:00
|
|
|
|
var logger = LogManager.GetLogger(type);
|
2015-11-08 01:32:58 +08:00
|
|
|
|
System.Diagnostics.Debug.WriteLine($"DEBUG: {msg}");
|
2015-01-20 22:33:45 +08:00
|
|
|
|
logger.Debug(msg);
|
2014-03-23 17:43:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-30 08:39:03 +08:00
|
|
|
|
public static void Debug(string msg)
|
2014-03-23 17:43:46 +08:00
|
|
|
|
{
|
2016-04-28 05:51:31 +08:00
|
|
|
|
var type = CallerType();
|
2016-11-30 08:39:03 +08:00
|
|
|
|
Debug(type, msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Info(string type, string msg)
|
|
|
|
|
{
|
2016-04-28 05:51:31 +08:00
|
|
|
|
var logger = LogManager.GetLogger(type);
|
2015-11-08 01:32:58 +08:00
|
|
|
|
System.Diagnostics.Debug.WriteLine($"INFO: {msg}");
|
2015-01-20 22:33:45 +08:00
|
|
|
|
logger.Info(msg);
|
2014-03-23 17:43:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-30 08:39:03 +08:00
|
|
|
|
public static void Info(string msg)
|
|
|
|
|
{
|
|
|
|
|
var type = CallerType();
|
|
|
|
|
Info(type, msg);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-23 17:43:46 +08:00
|
|
|
|
public static void Warn(string msg)
|
|
|
|
|
{
|
2016-04-28 05:51:31 +08:00
|
|
|
|
var type = CallerType();
|
|
|
|
|
var logger = LogManager.GetLogger(type);
|
2015-11-08 01:32:58 +08:00
|
|
|
|
System.Diagnostics.Debug.WriteLine($"WARN: {msg}");
|
2015-01-20 22:33:45 +08:00
|
|
|
|
logger.Warn(msg);
|
2014-03-23 17:43:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-09 09:32:33 +08:00
|
|
|
|
public static void Fatal(System.Exception e)
|
2014-03-23 17:43:46 +08:00
|
|
|
|
{
|
2015-11-08 01:32:58 +08:00
|
|
|
|
#if DEBUG
|
|
|
|
|
throw e;
|
|
|
|
|
#else
|
2016-05-18 05:48:18 +08:00
|
|
|
|
var type = CallerType();
|
|
|
|
|
var logger = LogManager.GetLogger(type);
|
2015-11-08 01:32:58 +08:00
|
|
|
|
logger.Fatal(ExceptionFormatter.FormatExcpetion(e));
|
|
|
|
|
#endif
|
2014-03-23 17:43:46 +08:00
|
|
|
|
}
|
2013-12-20 19:38:10 +08:00
|
|
|
|
}
|
2016-11-30 08:30:42 +08:00
|
|
|
|
}
|