PowerToys/Wox.Infrastructure/Logger/Log.cs

165 lines
5.4 KiB
C#
Raw Normal View History

2019-08-20 19:29:30 +08:00
using System.Diagnostics;
using System.IO;
2016-11-30 08:30:42 +08:00
using System.Runtime.CompilerServices;
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
{
public static class Log
2013-12-20 19:38:10 +08:00
{
public const string DirectoryName = "Logs";
static Log()
{
var path = Path.Combine(Constant.DataDirectory, DirectoryName, Constant.Version);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
2013-12-20 19:38:10 +08:00
var configuration = new LoggingConfiguration();
var target = new FileTarget();
configuration.AddTarget("file", target);
2019-08-20 19:29:30 +08:00
target.FileName = path.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
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)
{
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-05-16 00:03:06 +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('|');
var valid = parts.Length == 3 && !string.IsNullOrWhiteSpace(parts[1]) && !string.IsNullOrWhiteSpace(parts[2]);
return valid;
}
/// <param name="message">example: "|prefix|unprefixed" </param>
public static void Error(string message)
{
if (FormatValid(message))
2017-01-24 08:24:20 +08:00
{
var parts = message.Split('|');
var prefix = parts[1];
var unprefixed = parts[2];
var logger = LogManager.GetLogger(prefix);
2017-01-24 08:24:20 +08:00
System.Diagnostics.Debug.WriteLine($"ERROR|{message}");
logger.Error(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
}
/// <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
if (FormatValid(message))
2015-11-11 08:42:49 +08:00
{
var parts = message.Split('|');
var prefix = parts[1];
var unprefixed = parts[2];
var logger = LogManager.GetLogger(prefix);
2015-01-11 21:52:30 +08:00
2017-01-24 08:24:20 +08:00
System.Diagnostics.Debug.WriteLine($"ERROR|{message}");
2014-03-23 17:43:46 +08:00
2017-01-24 08:24:20 +08:00
logger.Error("-------------------------- Begin exception --------------------------");
logger.Error(unprefixed);
2016-11-30 08:39:03 +08:00
2017-01-24 08:24:20 +08:00
do
{
2019-12-10 18:18:24 +08:00
logger.Error($"Exception full name:\n <{e.GetType().FullName}>");
2017-01-24 08:24:20 +08:00
logger.Error($"Exception message:\n <{e.Message}>");
logger.Error($"Exception stack trace:\n <{e.StackTrace}>");
2017-03-05 00:41:03 +08:00
logger.Error($"Exception source:\n <{e.Source}>");
logger.Error($"Exception target site:\n <{e.TargetSite}>");
2017-04-02 01:56:29 +08:00
logger.Error($"Exception HResult:\n <{e.HResult}>");
2017-01-24 08:24:20 +08:00
e = e.InnerException;
} while (e != null);
logger.Error("-------------------------- End exception --------------------------");
}
else
{
LogFaultyFormat(message);
}
#endif
2014-03-23 17:43:46 +08:00
}
/// <param name="message">example: "|prefix|unprefixed" </param>
2017-01-24 08:24:20 +08:00
public static void Debug(string message)
2016-11-30 08:39:03 +08:00
{
if (FormatValid(message))
2017-01-24 08:24:20 +08:00
{
var parts = message.Split('|');
var prefix = parts[1];
var unprefixed = parts[2];
var logger = LogManager.GetLogger(prefix);
2017-01-24 08:24:20 +08:00
System.Diagnostics.Debug.WriteLine($"DEBUG|{message}");
logger.Debug(unprefixed);
2017-01-24 08:24:20 +08:00
}
else
{
LogFaultyFormat(message);
}
2016-11-30 08:39:03 +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
{
if (FormatValid(message))
2017-01-24 08:24:20 +08:00
{
var parts = message.Split('|');
var prefix = parts[1];
var unprefixed = parts[2];
var logger = LogManager.GetLogger(prefix);
2017-01-24 08:24:20 +08:00
System.Diagnostics.Debug.WriteLine($"INFO|{message}");
logger.Info(unprefixed);
2017-01-24 08:24:20 +08:00
}
else
{
LogFaultyFormat(message);
}
2014-03-23 17:43:46 +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
{
if (FormatValid(message))
2017-01-24 08:24:20 +08:00
{
var parts = message.Split('|');
var prefix = parts[1];
var unprefixed = parts[2];
var logger = LogManager.GetLogger(prefix);
2017-01-24 08:24:20 +08:00
System.Diagnostics.Debug.WriteLine($"WARN|{message}");
logger.Warn(unprefixed);
2017-01-24 08:24:20 +08:00
}
else
{
LogFaultyFormat(message);
}
2014-03-23 17:43:46 +08:00
}
2013-12-20 19:38:10 +08:00
}
2016-11-30 08:30:42 +08:00
}