From 5ab33e831d93759bd78f85a2caaa3546511517e4 Mon Sep 17 00:00:00 2001 From: bao-qian Date: Wed, 27 Apr 2016 22:51:31 +0100 Subject: [PATCH] Fix logger name and move logger to %APPDATA% --- Wox.Infrastructure/Logger/Log.cs | 45 ++++++++++++++++++-- Wox.Infrastructure/NLog.config | 23 ---------- Wox.Infrastructure/SyntaxSuger.cs | 7 ++- Wox.Infrastructure/Wox.Infrastructure.csproj | 5 --- 4 files changed, 46 insertions(+), 34 deletions(-) delete mode 100644 Wox.Infrastructure/NLog.config diff --git a/Wox.Infrastructure/Logger/Log.cs b/Wox.Infrastructure/Logger/Log.cs index 92f84cfaa2..4de1aead81 100644 --- a/Wox.Infrastructure/Logger/Log.cs +++ b/Wox.Infrastructure/Logger/Log.cs @@ -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 diff --git a/Wox.Infrastructure/NLog.config b/Wox.Infrastructure/NLog.config deleted file mode 100644 index 896abfdf6c..0000000000 --- a/Wox.Infrastructure/NLog.config +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - \ No newline at end of file diff --git a/Wox.Infrastructure/SyntaxSuger.cs b/Wox.Infrastructure/SyntaxSuger.cs index 40be92fe29..0476d337b2 100644 --- a/Wox.Infrastructure/SyntaxSuger.cs +++ b/Wox.Infrastructure/SyntaxSuger.cs @@ -2,9 +2,12 @@ namespace Wox.Infrastructure { - static class SyntaxSuger + static class Helper { - public static T RequireNonNull(T obj) + /// + /// http://www.yinwang.org/blog-cn/2015/11/21/programming-philosophy + /// + public static T RequireNonNull(this T obj) { if (obj == null) { diff --git a/Wox.Infrastructure/Wox.Infrastructure.csproj b/Wox.Infrastructure/Wox.Infrastructure.csproj index b8f3f47178..13b0626f52 100644 --- a/Wox.Infrastructure/Wox.Infrastructure.csproj +++ b/Wox.Infrastructure/Wox.Infrastructure.csproj @@ -98,11 +98,6 @@ Wox.Plugin - - - Always - -