Fix logger name and move logger to %APPDATA%

This commit is contained in:
bao-qian
2016-04-27 22:51:31 +01:00
parent dc3b01dc15
commit 5ab33e831d
4 changed files with 46 additions and 34 deletions

View File

@@ -1,14 +1,43 @@
using NLog;
using Wox.Infrastructure.Exception;
using System.Diagnostics;
using System.IO;
using NLog;
using NLog.Config;
using NLog.Targets;
namespace Wox.Infrastructure.Logger
{
public class Log
public static class Log
{
private static NLog.Logger logger = LogManager.GetCurrentClassLogger();
static Log()
{
var directoryName = "Logs";
var path = Path.Combine(Wox.DataPath, directoryName);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
var configuration = new LoggingConfiguration();
var target = new FileTarget();
configuration.AddTarget("file", target);
target.FileName = "${specialfolder:folder=ApplicationData}/" + Wox.Name + "/" + directoryName + "/${shortdate}.log";
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().RequireNonNull();
var callingFrame = stackFrames[2];
var method = callingFrame.GetMethod();
var type = $"{method.DeclaringType.RequireNonNull().FullName}.{method.Name}";
return type;
}
public static void Error(System.Exception e)
{
var type = CallerType();
var logger = LogManager.GetLogger(type);
#if DEBUG
throw e;
#else
@@ -23,24 +52,32 @@ namespace Wox.Infrastructure.Logger
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)
{
var type = CallerType();
var logger = LogManager.GetLogger(type);
#if DEBUG
throw e;
#else