PowerToys/Wox.Infrastructure/Logger/Log.cs
bao-qian 245cffc4f7 Refactoring visibility related events
1. remove unnecessary events from MainViewModel
2. remove usage of Obsolete api (show, hide etc)
3. fix space problem in #660
4. part of #486
5. fix up/down key bug introduced in 92b7ca6a1bafd254e39ee92812ff691906cd85c1
6. fix #678
2016-05-25 01:19:46 +01:00

101 lines
3.0 KiB
C#

using System.Diagnostics;
using System.IO;
using NLog;
using NLog.Config;
using NLog.Targets;
using Wox.Infrastructure.Exception;
namespace Wox.Infrastructure.Logger
{
public static class Log
{
public const string DirectoryName = "Logs";
static Log()
{
var path = Path.Combine(Constant.DataDirectory, DirectoryName, Constant.Version);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
var configuration = new LoggingConfiguration();
var target = new FileTarget();
configuration.AddTarget("file", target);
target.FileName = "${specialfolder:folder=ApplicationData}/" + Constant.Wox + "/" + DirectoryName + "/" + Constant.Version + "/${shortdate}.txt";
var rule = new LoggingRule("*", LogLevel.Info, target);
configuration.LoggingRules.Add(rule);
LogManager.Configuration = configuration;
}
private static string CallerType()
{
var stackTrace = new StackTrace();
var stackFrames = stackTrace.GetFrames().NonNull();
var callingFrame = stackFrames[2];
var method = callingFrame.GetMethod();
var type = $"{method.DeclaringType.NonNull().FullName}.{method.Name}";
return type;
}
public static void Error(string msg)
{
var type = CallerType();
var logger = LogManager.GetLogger(type);
System.Diagnostics.Debug.WriteLine($"ERROR: {msg}");
logger.Error(msg);
}
public static void Exception(System.Exception e)
{
#if DEBUG
throw e;
#else
var type = CallerType();
var logger = LogManager.GetLogger(type);
do
{
logger.Error(e.Message);
logger.Error($"\n{e.StackTrace}");
e = e.InnerException;
} while (e != null);
#endif
}
public static void Debug(string msg)
{
var type = CallerType();
var logger = LogManager.GetLogger(type);
System.Diagnostics.Debug.WriteLine($"DEBUG: {msg}");
logger.Debug(msg);
}
public static void Info(string msg)
{
var type = CallerType();
var logger = LogManager.GetLogger(type);
System.Diagnostics.Debug.WriteLine($"INFO: {msg}");
logger.Info(msg);
}
public static void Warn(string msg)
{
var type = CallerType();
var logger = LogManager.GetLogger(type);
System.Diagnostics.Debug.WriteLine($"WARN: {msg}");
logger.Warn(msg);
}
public static void Fatal(System.Exception e)
{
#if DEBUG
throw e;
#else
var type = CallerType();
var logger = LogManager.GetLogger(type);
logger.Fatal(ExceptionFormatter.FormatExcpetion(e));
#endif
}
}
}