2019-08-20 19:29:30 +08:00
|
|
|
using System.Diagnostics;
|
2016-04-28 05:51:31 +08:00
|
|
|
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;
|
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";
|
|
|
|
|
2020-01-09 07:13:56 +08:00
|
|
|
public static string CurrentLogDirectory { get; private set; }
|
|
|
|
|
2016-04-28 05:51:31 +08:00
|
|
|
static Log()
|
|
|
|
{
|
2020-01-09 07:13:56 +08:00
|
|
|
CurrentLogDirectory = Path.Combine(Constant.DataDirectory, DirectoryName, Constant.Version);
|
|
|
|
if (!Directory.Exists(CurrentLogDirectory))
|
2016-04-28 05:51:31 +08:00
|
|
|
{
|
2020-01-09 07:13:56 +08:00
|
|
|
Directory.CreateDirectory(CurrentLogDirectory);
|
2016-04-28 05:51:31 +08:00
|
|
|
}
|
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);
|
2020-01-09 07:13:56 +08:00
|
|
|
target.FileName = CurrentLogDirectory.Replace(@"\", "/") + "/${shortdate}.txt";
|
2017-01-13 03:52:37 +08:00
|
|
|
#if DEBUG
|
|
|
|
var rule = new LoggingRule("*", LogLevel.Debug, target);
|
|
|
|
#else
|
2017-03-31 02:52:58 +08:00
|
|
|
var rule = new LoggingRule("*", LogLevel.Info, target);
|
2017-01-13 03:52:37 +08:00
|
|
|
#endif
|
2016-04-28 05:51:31 +08:00
|
|
|
configuration.LoggingRules.Add(rule);
|
|
|
|
LogManager.Configuration = configuration;
|
|
|
|
}
|
2016-11-30 08:30:42 +08:00
|
|
|
|
2017-01-24 08:24:20 +08:00
|
|
|
private static void LogFaultyFormat(string message)
|
2016-04-28 05:51:31 +08:00
|
|
|
{
|
2017-01-24 08:24:20 +08:00
|
|
|
var logger = LogManager.GetLogger("FaultyLogger");
|
|
|
|
message = $"Wrong logger message format <{message}>";
|
|
|
|
System.Diagnostics.Debug.WriteLine($"FATAL|{message}");
|
|
|
|
logger.Fatal(message);
|
2016-04-28 05:51:31 +08:00
|
|
|
}
|
2016-05-16 00:03:06 +08:00
|
|
|
|
2017-01-25 08:24:00 +08:00
|
|
|
private static bool FormatValid(string message)
|
2015-01-11 21:52:30 +08:00
|
|
|
{
|
2017-01-24 08:24:20 +08:00
|
|
|
var parts = message.Split('|');
|
2017-01-25 08:24:00 +08:00
|
|
|
var valid = parts.Length == 3 && !string.IsNullOrWhiteSpace(parts[1]) && !string.IsNullOrWhiteSpace(parts[2]);
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
2019-12-30 07:13:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.Synchronized)]
|
|
|
|
public static void Exception(string className, string message, System.Exception exception, [CallerMemberName] string methodName = "")
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(className))
|
|
|
|
{
|
|
|
|
LogFaultyFormat($"Fail to specify a class name during logging of message: {message ?? "no message entered"}");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(message))
|
|
|
|
{ // todo: not sure we really need that
|
|
|
|
LogFaultyFormat($"Fail to specify a message during logging");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(methodName))
|
|
|
|
{
|
|
|
|
className += "." + methodName;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExceptionInternal(className, message, exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void ExceptionInternal(string classAndMethod, string message, System.Exception e)
|
|
|
|
{
|
|
|
|
var logger = LogManager.GetLogger(classAndMethod);
|
|
|
|
|
|
|
|
System.Diagnostics.Debug.WriteLine($"ERROR|{message}");
|
|
|
|
|
|
|
|
logger.Error("-------------------------- Begin exception --------------------------");
|
|
|
|
logger.Error(message);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
logger.Error($"Exception full name:\n <{e.GetType().FullName}>");
|
|
|
|
logger.Error($"Exception message:\n <{e.Message}>");
|
|
|
|
logger.Error($"Exception stack trace:\n <{e.StackTrace}>");
|
|
|
|
logger.Error($"Exception source:\n <{e.Source}>");
|
|
|
|
logger.Error($"Exception target site:\n <{e.TargetSite}>");
|
|
|
|
logger.Error($"Exception HResult:\n <{e.HResult}>");
|
|
|
|
e = e.InnerException;
|
|
|
|
} while (e != null);
|
|
|
|
|
|
|
|
logger.Error("-------------------------- End exception --------------------------");
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void LogInternal(string message, LogLevel level)
|
2017-01-25 08:24:00 +08:00
|
|
|
{
|
|
|
|
if (FormatValid(message))
|
2017-01-24 08:24:20 +08:00
|
|
|
{
|
2017-01-25 08:24:00 +08:00
|
|
|
var parts = message.Split('|');
|
|
|
|
var prefix = parts[1];
|
|
|
|
var unprefixed = parts[2];
|
|
|
|
var logger = LogManager.GetLogger(prefix);
|
|
|
|
|
2019-12-30 07:13:33 +08:00
|
|
|
System.Diagnostics.Debug.WriteLine($"{level.Name}|{message}");
|
|
|
|
logger.Log(level, unprefixed);
|
2017-01-24 08:24:20 +08:00
|
|
|
}
|
|
|
|
else
|
2016-11-30 08:30:42 +08:00
|
|
|
{
|
2017-01-24 08:24:20 +08:00
|
|
|
LogFaultyFormat(message);
|
|
|
|
}
|
2016-11-30 08:30:42 +08:00
|
|
|
}
|
|
|
|
|
2017-01-25 08:24:00 +08:00
|
|
|
/// <param name="message">example: "|prefix|unprefixed" </param>
|
2016-11-30 08:30:42 +08:00
|
|
|
[MethodImpl(MethodImplOptions.Synchronized)]
|
2017-01-24 08:24:20 +08:00
|
|
|
public static void Exception(string message, System.Exception e)
|
2016-05-16 00:03:06 +08:00
|
|
|
{
|
2017-01-24 08:24:20 +08:00
|
|
|
#if DEBUG
|
|
|
|
throw e;
|
|
|
|
#else
|
2017-01-25 08:24:00 +08:00
|
|
|
if (FormatValid(message))
|
2015-11-11 08:42:49 +08:00
|
|
|
{
|
2017-01-25 08:24:00 +08:00
|
|
|
var parts = message.Split('|');
|
|
|
|
var prefix = parts[1];
|
|
|
|
var unprefixed = parts[2];
|
2019-12-30 07:13:33 +08:00
|
|
|
ExceptionInternal(prefix, unprefixed, e);
|
2017-01-24 08:24:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogFaultyFormat(message);
|
|
|
|
}
|
|
|
|
#endif
|
2014-03-23 17:43:46 +08:00
|
|
|
}
|
2019-12-30 07:13:33 +08:00
|
|
|
|
2017-01-25 08:24:00 +08:00
|
|
|
/// <param name="message">example: "|prefix|unprefixed" </param>
|
2019-12-30 07:13:33 +08:00
|
|
|
public static void Error(string message)
|
2016-11-30 08:39:03 +08:00
|
|
|
{
|
2019-12-30 07:13:33 +08:00
|
|
|
LogInternal(message, LogLevel.Error);
|
|
|
|
}
|
2017-01-25 08:24:00 +08:00
|
|
|
|
2019-12-30 07:13:33 +08:00
|
|
|
/// <param name="message">example: "|prefix|unprefixed" </param>
|
|
|
|
public static void Debug(string message)
|
|
|
|
{
|
|
|
|
LogInternal(message, LogLevel.Debug);
|
2016-11-30 08:39:03 +08:00
|
|
|
}
|
|
|
|
|
2017-01-25 08:24:00 +08:00
|
|
|
/// <param name="message">example: "|prefix|unprefixed" </param>
|
2017-01-24 08:24:20 +08:00
|
|
|
public static void Info(string message)
|
2014-03-23 17:43:46 +08:00
|
|
|
{
|
2019-12-30 07:13:33 +08:00
|
|
|
LogInternal(message, LogLevel.Info);
|
2014-03-23 17:43:46 +08:00
|
|
|
}
|
|
|
|
|
2017-01-25 08:24:00 +08:00
|
|
|
/// <param name="message">example: "|prefix|unprefixed" </param>
|
2017-01-24 08:24:20 +08:00
|
|
|
public static void Warn(string message)
|
2014-03-23 17:43:46 +08:00
|
|
|
{
|
2019-12-30 07:13:33 +08:00
|
|
|
LogInternal(message, LogLevel.Warn);
|
2014-03-23 17:43:46 +08:00
|
|
|
}
|
2013-12-20 19:38:10 +08:00
|
|
|
}
|
2016-11-30 08:30:42 +08:00
|
|
|
}
|