Rewrite all log message format

This commit is contained in:
bao-qian
2017-01-24 00:24:20 +00:00
parent fdfd684e7a
commit 045fd20d8c
25 changed files with 180 additions and 186 deletions

View File

@@ -76,7 +76,7 @@ namespace Wox.Infrastructure.Image
}
catch (System.Exception e)
{
Log.Exception(e);
Log.Exception($"|ImageLoader.ShellIcon|can't get shell icon for <{fileName}>", e);
return ImageCache[Constant.ErrorIcon];
}
}
@@ -91,7 +91,7 @@ namespace Wox.Infrastructure.Image
}
Task.Run(() =>
{
Stopwatch.Normal("Preload images from cache", () =>
Stopwatch.Normal("|ImageLoader.PreLoadImages|Preload images cost", () =>
{
ImageCache.Usage.AsParallel().Where(i => !ImageCache.ContainsKey(i.Key)).ForAll(i =>
{
@@ -102,7 +102,7 @@ namespace Wox.Infrastructure.Image
}
});
});
Log.Info($"Preload {ImageCache.Usage.Count} images from cache");
Log.Info($"|ImageLoader.PreLoadImages|Number of preload images is <{ImageCache.Usage.Count}>");
});
}

View File

@@ -4,7 +4,6 @@ using System.Runtime.CompilerServices;
using NLog;
using NLog.Config;
using NLog.Targets;
using Wox.Infrastructure.Exception;
namespace Wox.Infrastructure.Logger
{
@@ -34,98 +33,104 @@ namespace Wox.Infrastructure.Logger
LogManager.Configuration = configuration;
}
public static string CallerType()
private static void LogFaultyFormat(string message)
{
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;
var logger = LogManager.GetLogger("FaultyLogger");
message = $"Wrong logger message format <{message}>";
System.Diagnostics.Debug.WriteLine($"FATAL|{message}");
logger.Fatal(message);
}
public static void Error(string msg)
public static void Error(string message)
{
var type = CallerType();
var logger = LogManager.GetLogger(type);
System.Diagnostics.Debug.WriteLine($"ERROR: {msg}");
logger.Error(msg);
var parts = message.Split('|');
if (parts.Length == 3 && !string.IsNullOrWhiteSpace(parts[1]) && !string.IsNullOrWhiteSpace(parts[2]))
{
var logger = LogManager.GetLogger(parts[1]);
System.Diagnostics.Debug.WriteLine($"ERROR|{message}");
logger.Error(parts[2]);
}
else
{
LogFaultyFormat(message);
}
}
[MethodImpl(MethodImplOptions.Synchronized)]
public static void Error(System.Exception e, string msg)
{
var type = CallerType();
var logger = LogManager.GetLogger(type);
System.Diagnostics.Debug.WriteLine($"ERROR: {msg}");
logger.Error("-------------------------- Begin exception --------------------------");
logger.Error(msg);
do
{
logger.Error($"Exception message:\n <{e.Message}>");
logger.Error($"Exception stack trace:\n<{e.StackTrace}>");
e = e.InnerException;
} while (e != null);
logger.Error("-------------------------- End exception --------------------------");
}
[MethodImpl(MethodImplOptions.Synchronized)]
public static void Exception(System.Exception e)
{
var type = CallerType();
var logger = LogManager.GetLogger(type);
do
{
logger.Error($"Exception message:\n <{e.Message}>");
logger.Error($"Exception stack trace:\n<{e.StackTrace}>");
e = e.InnerException;
} while (e != null);
}
public static void Debug(string type, string msg)
{
var logger = LogManager.GetLogger(type);
System.Diagnostics.Debug.WriteLine($"DEBUG: {msg}");
logger.Debug(msg);
}
public static void Debug(string msg)
{
var type = CallerType();
Debug(type, msg);
}
public static void Info(string type, string msg)
{
var logger = LogManager.GetLogger(type);
System.Diagnostics.Debug.WriteLine($"INFO: {msg}");
logger.Info(msg);
}
public static void Info(string msg)
{
var type = CallerType();
Info(type, 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)
public static void Exception(string message, System.Exception e)
{
#if DEBUG
throw e;
#else
var type = CallerType();
var logger = LogManager.GetLogger(type);
logger.Fatal(ExceptionFormatter.FormatExcpetion(e));
var parts = message.Split('|');
if (parts.Length == 3 && !string.IsNullOrWhiteSpace(parts[1]) && !string.IsNullOrWhiteSpace(parts[2]))
{
var logger = LogManager.GetLogger(parts[1]);
System.Diagnostics.Debug.WriteLine($"ERROR|{message}");
logger.Error("-------------------------- Begin exception --------------------------");
logger.Error(parts[2]);
do
{
logger.Error($"Exception message:\n <{e.Message}>");
logger.Error($"Exception stack trace:\n<{e.StackTrace}>");
e = e.InnerException;
} while (e != null);
logger.Error("-------------------------- End exception --------------------------");
}
else
{
LogFaultyFormat(message);
}
#endif
}
public static void Debug(string message)
{
var parts = message.Split('|');
if (parts.Length == 3 && !string.IsNullOrWhiteSpace(parts[1]) && !string.IsNullOrWhiteSpace(parts[2]))
{
var logger = LogManager.GetLogger(parts[1]);
System.Diagnostics.Debug.WriteLine($"DEBUG|{message}");
logger.Debug(parts[2]);
}
else
{
LogFaultyFormat(message);
}
}
public static void Info(string message)
{
var parts = message.Split('|');
if (parts.Length == 3 && !string.IsNullOrWhiteSpace(parts[1]) && !string.IsNullOrWhiteSpace(parts[2]))
{
var logger = LogManager.GetLogger(parts[1]);
System.Diagnostics.Debug.WriteLine($"INFO|{message}");
logger.Info(parts[2]);
}
else
{
LogFaultyFormat(message);
}
}
public static void Warn(string message)
{
var parts = message.Split('|');
if (parts.Length == 3 && !string.IsNullOrWhiteSpace(parts[1]) && !string.IsNullOrWhiteSpace(parts[2]))
{
var logger = LogManager.GetLogger(parts[1]);
System.Diagnostics.Debug.WriteLine($"WARN|{message}");
logger.Warn(parts[2]);
}
else
{
LogFaultyFormat(message);
}
}
}
}

View File

@@ -11,29 +11,27 @@ namespace Wox.Infrastructure
/// <summary>
/// This stopwatch will appear only in Debug mode
/// </summary>
public static long Debug(string name, Action action)
public static long Debug(string message, Action action)
{
var stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
action();
stopWatch.Stop();
var milliseconds = stopWatch.ElapsedMilliseconds;
string info = $"{name} : {milliseconds}ms";
var type = Log.CallerType();
Log.Debug(type, info);
string info = $"{message} <{milliseconds}ms>";
Log.Debug(info);
return milliseconds;
}
public static long Normal(string name, Action action)
public static long Normal(string message, Action action)
{
var stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
action();
stopWatch.Stop();
var milliseconds = stopWatch.ElapsedMilliseconds;
string info = $"{name} : {milliseconds}ms";
var type = Log.CallerType();
Log.Info(type, info);
string info = $"{message} <{milliseconds}ms>";
Log.Info(info);
return milliseconds;
}

View File

@@ -39,7 +39,7 @@ namespace Wox.Infrastructure.Storage
}
else
{
Log.Error($"Zero length cache file: {FilePath}");
Log.Error($"|BinaryStorage.TryLoad|Zero length cache file <{FilePath}>");
Save(defaultData);
return defaultData;
}
@@ -47,7 +47,7 @@ namespace Wox.Infrastructure.Storage
}
else
{
Log.Info("Cache file not exist, load default data");
Log.Info("|BinaryStorage.TryLoad|Cache file not exist, load default data");
Save(defaultData);
return defaultData;
}
@@ -64,13 +64,12 @@ namespace Wox.Infrastructure.Storage
try
{
var t = (T) binaryFormatter.Deserialize(stream);
var t = (T)binaryFormatter.Deserialize(stream);
return t;
}
catch (System.Exception e)
{
Log.Error($"Deserialize error for cache file: {FilePath}");
Log.Exception(e);
Log.Exception($"|BinaryStorage.Deserialize|Deserialize error for file <{FilePath}>", e);
return defaultData;
}
finally
@@ -110,8 +109,7 @@ namespace Wox.Infrastructure.Storage
}
catch (SerializationException e)
{
Log.Error($"Serialize error for cache file: {FilePath}");
Log.Exception(e);
Log.Exception($"|BinaryStorage.Save|serialize error for file <{FilePath}>", e);
}
}
}

View File

@@ -60,7 +60,7 @@ namespace Wox.Infrastructure.Storage
catch (JsonSerializationException e)
{
LoadDefault();
Log.Exception(e);
Log.Exception($"|JsonStrorage.Deserialize|Deserialize error for json <{FilePath}>", e);
}
}