mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 11:46:30 +02:00
Improve logging for PT Run (#6800)
* init code pass * adjusting tabbing for readabilty * few small adjustments
This commit is contained in:
@@ -41,7 +41,7 @@ namespace Wox.Infrastructure
|
||||
// force pinyin library static constructor initialize
|
||||
PinyinHelper.toHanyuPinyinStringArray('T', _pinyinFormat);
|
||||
});
|
||||
Log.Info($"|Wox.Infrastructure.Alphabet.Initialize|Number of preload pinyin combination<{_pinyinCache.Count}>");
|
||||
Log.Info($"Number of preload pinyin combination<{_pinyinCache.Count}>", GetType());
|
||||
}
|
||||
|
||||
public string Translate(string str)
|
||||
|
||||
@@ -21,7 +21,8 @@ namespace Wox.Infrastructure.FileSystemHelper
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
Log.Info($"File {path} is being accessed by another process| {ex.Message}");
|
||||
Log.Info($"File {path} is being accessed by another process| {ex.Message}", GetType());
|
||||
|
||||
return new string[] { string.Empty };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Wox.Infrastructure.Logger;
|
||||
@@ -95,7 +96,7 @@ namespace Wox.Infrastructure
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.Exception($"Wox.Infrastructure.Helper| Unable to Run {path} as admin : {ex.Message}", ex);
|
||||
Log.Exception($"Unable to Run {path} as admin : {ex.Message}", ex, MethodBase.GetCurrentMethod().DeclaringType);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using JetBrains.Annotations;
|
||||
@@ -61,7 +62,8 @@ namespace Wox.Infrastructure.Http
|
||||
|
||||
public static async Task<string> Get([NotNull] string url, string encoding = "UTF-8")
|
||||
{
|
||||
Log.Debug($"|Http.Get|Url <{url}>");
|
||||
Log.Debug($"Url <{url}>", MethodBase.GetCurrentMethod().DeclaringType);
|
||||
|
||||
var request = WebRequest.CreateHttp(url);
|
||||
request.Method = "GET";
|
||||
request.Timeout = 1000;
|
||||
|
||||
@@ -7,6 +7,7 @@ using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
@@ -62,7 +63,8 @@ namespace Wox.Infrastructure.Image
|
||||
Load(x.Key);
|
||||
});
|
||||
});
|
||||
Log.Info($"|ImageLoader.Initialize|Number of preload images is <{ImageCache.Usage.Count}>, Images Number: {ImageCache.CacheSize()}, Unique Items {ImageCache.UniqueImagesInCache()}");
|
||||
|
||||
Log.Info($"Number of preload images is <{ImageCache.Usage.Count}>, Images Number: {ImageCache.CacheSize()}, Unique Items {ImageCache.UniqueImagesInCache()}", MethodBase.GetCurrentMethod().DeclaringType);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -188,7 +190,7 @@ namespace Wox.Infrastructure.Image
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path}", e);
|
||||
Log.Exception($"Failed to get thumbnail for {path}", e, MethodBase.GetCurrentMethod().DeclaringType);
|
||||
type = ImageType.Error;
|
||||
image = ImageCache[ErrorIconPath];
|
||||
ImageCache[path] = image;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using NLog;
|
||||
@@ -37,67 +38,25 @@ namespace Wox.Infrastructure.Logger
|
||||
LogManager.Configuration = configuration;
|
||||
}
|
||||
|
||||
private static void LogFaultyFormat(string message)
|
||||
private static void LogInternalException(string message, System.Exception e, Type fullClassName, [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
|
||||
{
|
||||
var logger = LogManager.GetLogger("FaultyLogger");
|
||||
message = $"Wrong logger message format <{message}>";
|
||||
System.Diagnostics.Debug.WriteLine($"FATAL|{message}");
|
||||
logger.Fatal(message);
|
||||
}
|
||||
var logger = GetLogger(fullClassName.FullName, methodName);
|
||||
|
||||
private static bool FormatValid(string message)
|
||||
{
|
||||
var parts = message.Split('|');
|
||||
var valid = parts.Length == 3 && !string.IsNullOrWhiteSpace(parts[1]) && !string.IsNullOrWhiteSpace(parts[2]);
|
||||
return valid;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.Synchronized)]
|
||||
public static void Exception(string className, string message, System.Exception exception, [CallerMemberName] string methodName = "")
|
||||
{
|
||||
var classNameWithMethod = CheckClassAndMessageAndReturnFullClassWithMethod(className, message, methodName);
|
||||
|
||||
ExceptionInternal(classNameWithMethod, message, exception);
|
||||
}
|
||||
|
||||
private static string CheckClassAndMessageAndReturnFullClassWithMethod(string className, string message, string methodName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(className))
|
||||
{
|
||||
LogFaultyFormat($"Fail to specify a class name during logging of message: {message ?? "no message entered"}");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(message))
|
||||
{
|
||||
// todo: not sure we really need that
|
||||
LogFaultyFormat($"Fail to specify a message during logging");
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(methodName))
|
||||
{
|
||||
return className + "." + methodName;
|
||||
}
|
||||
|
||||
return className;
|
||||
}
|
||||
|
||||
private static void ExceptionInternal(string classAndMethod, string message, System.Exception e)
|
||||
{
|
||||
var logger = LogManager.GetLogger(classAndMethod);
|
||||
|
||||
System.Diagnostics.Debug.WriteLine($"ERROR|{message}");
|
||||
LogInternal(LogLevel.Error, message, fullClassName, logger, methodName, sourceFilePath, sourceLineNumber);
|
||||
|
||||
logger.Error("-------------------------- Begin exception --------------------------");
|
||||
logger.Error(message);
|
||||
logger.Error($"\n\tMessage:\n\t {message}");
|
||||
|
||||
do
|
||||
{
|
||||
logger.Error($"Exception full name:\n <{e.GetType().FullName}>");
|
||||
logger.Error($"Exception message:\n <{e.Message}>");
|
||||
logger.Error($"Exception stack trace:\n <{e.StackTrace}>");
|
||||
logger.Error($"Exception source:\n <{e.Source}>");
|
||||
logger.Error($"Exception target site:\n <{e.TargetSite}>");
|
||||
logger.Error($"Exception HResult:\n <{e.HResult}>");
|
||||
logger.Error(
|
||||
$"\n\tException full name:\n\t <{e.GetType().FullName}>" +
|
||||
$"\n\tException message:\n\t <{e.Message}>" +
|
||||
$"\n\tException stack trace:\n\t <{e.StackTrace}>" +
|
||||
$"\n\tException source:\n\t <{e.Source}>" +
|
||||
$"\n\tException target site:\n\t <{e.TargetSite}>" +
|
||||
$"\n\tException HResult:\n\t <{e.HResult}>");
|
||||
|
||||
e = e.InnerException;
|
||||
}
|
||||
while (e != null);
|
||||
@@ -105,93 +64,53 @@ namespace Wox.Infrastructure.Logger
|
||||
logger.Error("-------------------------- End exception --------------------------");
|
||||
}
|
||||
|
||||
private static void LogInternal(string message, LogLevel level)
|
||||
public static void Info(string message, Type fullClassName, [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
|
||||
{
|
||||
if (FormatValid(message))
|
||||
{
|
||||
var parts = message.Split('|');
|
||||
var prefix = parts[1];
|
||||
var unprefixed = parts[2];
|
||||
var logger = LogManager.GetLogger(prefix);
|
||||
|
||||
System.Diagnostics.Debug.WriteLine($"{level.Name}|{message}");
|
||||
logger.Log(level, unprefixed);
|
||||
}
|
||||
else
|
||||
{
|
||||
LogFaultyFormat(message);
|
||||
}
|
||||
LogInternal(LogLevel.Info, message, fullClassName, methodName, sourceFilePath, sourceLineNumber);
|
||||
}
|
||||
|
||||
/// <param name="message">example: "|prefix|unprefixed" </param>
|
||||
[MethodImpl(MethodImplOptions.Synchronized)]
|
||||
public static void Exception(string message, System.Exception e)
|
||||
public static void Debug(string message, Type fullClassName, [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
|
||||
{
|
||||
if (FormatValid(message))
|
||||
{
|
||||
var parts = message.Split('|');
|
||||
var prefix = parts[1];
|
||||
var unprefixed = parts[2];
|
||||
ExceptionInternal(prefix, unprefixed, e);
|
||||
}
|
||||
else
|
||||
{
|
||||
LogFaultyFormat(message);
|
||||
}
|
||||
LogInternal(LogLevel.Debug, message, fullClassName, methodName, sourceFilePath, sourceLineNumber);
|
||||
}
|
||||
|
||||
/// <param name="message">example: "|prefix|unprefixed" </param>
|
||||
public static void Error(string message)
|
||||
public static void Warn(string message, Type fullClassName, [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
|
||||
{
|
||||
LogInternal(message, LogLevel.Error);
|
||||
LogInternal(LogLevel.Warn, message, fullClassName, methodName, sourceFilePath, sourceLineNumber);
|
||||
}
|
||||
|
||||
public static void Error(string className, string message, [CallerMemberName] string methodName = "")
|
||||
public static void Error(string message, Type fullClassName, [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
|
||||
{
|
||||
LogInternal(LogLevel.Error, className, message, methodName);
|
||||
LogInternal(LogLevel.Error, message, fullClassName, methodName, sourceFilePath, sourceLineNumber);
|
||||
}
|
||||
|
||||
private static void LogInternal(LogLevel level, string className, string message, [CallerMemberName] string methodName = "")
|
||||
public static void Exception(string message, System.Exception ex, Type fullClassName, [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
|
||||
{
|
||||
var classNameWithMethod = CheckClassAndMessageAndReturnFullClassWithMethod(className, message, methodName);
|
||||
|
||||
var logger = LogManager.GetLogger(classNameWithMethod);
|
||||
|
||||
System.Diagnostics.Debug.WriteLine($"{level.Name}|{message}");
|
||||
logger.Log(level, message);
|
||||
LogInternalException(message, ex, fullClassName, methodName, sourceFilePath, sourceLineNumber);
|
||||
}
|
||||
|
||||
public static void Debug(string className, string message, [CallerMemberName] string methodName = "")
|
||||
private static void LogInternal(LogLevel level, string message, Type fullClassName, [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
|
||||
{
|
||||
LogInternal(LogLevel.Debug, className, message, methodName);
|
||||
var logger = GetLogger(fullClassName.FullName, methodName);
|
||||
|
||||
LogInternal(level, message, fullClassName, logger, methodName, sourceFilePath, sourceLineNumber);
|
||||
}
|
||||
|
||||
/// <param name="message">example: "|prefix|unprefixed" </param>
|
||||
public static void Debug(string message)
|
||||
private static void LogInternal(LogLevel level, string message, Type fullClassName, NLog.Logger logger, [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
|
||||
{
|
||||
LogInternal(message, LogLevel.Debug);
|
||||
// System.Diagnostics.Debug.WriteLine($" {level.Name} | {message}");
|
||||
var msg = $"\n\tMessage: {message}" +
|
||||
$"\n\tArea: {fullClassName}.{methodName}" +
|
||||
$"\n\tSource Path: {sourceFilePath}::{sourceLineNumber}\n";
|
||||
|
||||
logger.Log(level, msg);
|
||||
}
|
||||
|
||||
public static void Info(string className, string message, [CallerMemberName] string methodName = "")
|
||||
private static NLog.Logger GetLogger(string fullClassName, string methodName)
|
||||
{
|
||||
LogInternal(LogLevel.Info, className, message, methodName);
|
||||
}
|
||||
var classNameWithMethod = $"{fullClassName}.{methodName}";
|
||||
|
||||
/// <param name="message">example: "|prefix|unprefixed" </param>
|
||||
public static void Info(string message)
|
||||
{
|
||||
LogInternal(message, LogLevel.Info);
|
||||
}
|
||||
|
||||
public static void Warn(string className, string message, [CallerMemberName] string methodName = "")
|
||||
{
|
||||
LogInternal(LogLevel.Warn, className, message, methodName);
|
||||
}
|
||||
|
||||
/// <param name="message">example: "|prefix|unprefixed" </param>
|
||||
public static void Warn(string message)
|
||||
{
|
||||
LogInternal(message, LogLevel.Warn);
|
||||
return LogManager.GetLogger(classNameWithMethod);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using Wox.Infrastructure.Logger;
|
||||
|
||||
namespace Wox.Infrastructure
|
||||
@@ -24,7 +25,7 @@ namespace Wox.Infrastructure
|
||||
stopWatch.Stop();
|
||||
var milliseconds = stopWatch.ElapsedMilliseconds;
|
||||
string info = $"{message} <{milliseconds}ms>";
|
||||
Log.Debug(info);
|
||||
Log.Debug(info, MethodBase.GetCurrentMethod().DeclaringType);
|
||||
return milliseconds;
|
||||
}
|
||||
|
||||
@@ -36,7 +37,7 @@ namespace Wox.Infrastructure
|
||||
stopWatch.Stop();
|
||||
var milliseconds = stopWatch.ElapsedMilliseconds;
|
||||
string info = $"{message} <{milliseconds}ms>";
|
||||
Log.Info(info);
|
||||
Log.Info(info, MethodBase.GetCurrentMethod().DeclaringType);
|
||||
return milliseconds;
|
||||
}
|
||||
|
||||
@@ -65,7 +66,7 @@ namespace Wox.Infrastructure
|
||||
foreach (var key in Count.Keys)
|
||||
{
|
||||
string info = $"{key} already cost {Count[key]}ms";
|
||||
Log.Debug(info);
|
||||
Log.Debug(info, MethodBase.GetCurrentMethod().DeclaringType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,8 @@ namespace Wox.Infrastructure.Storage
|
||||
if (File.Exists(FilePath))
|
||||
{
|
||||
File.Delete(FilePath);
|
||||
Log.Info($"|BinaryStorage.TryLoad|Deleting cached data at <{FilePath}>");
|
||||
|
||||
Log.Info($"Deleting cached data at <{FilePath}>", GetType());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,7 +53,8 @@ namespace Wox.Infrastructure.Storage
|
||||
{
|
||||
if (new FileInfo(FilePath).Length == 0)
|
||||
{
|
||||
Log.Error($"|BinaryStorage.TryLoad|Zero length cache file <{FilePath}>");
|
||||
Log.Error($"Zero length cache file <{FilePath}>", GetType());
|
||||
|
||||
Save(defaultData);
|
||||
return defaultData;
|
||||
}
|
||||
@@ -65,7 +67,8 @@ namespace Wox.Infrastructure.Storage
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Info("|BinaryStorage.TryLoad|Cache file not exist, load default data");
|
||||
Log.Info("Cache file not exist, load default data", GetType());
|
||||
|
||||
Save(defaultData);
|
||||
return defaultData;
|
||||
}
|
||||
@@ -87,7 +90,8 @@ namespace Wox.Infrastructure.Storage
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Exception($"|BinaryStorage.Deserialize|Deserialize error for file <{FilePath}>", e);
|
||||
Log.Exception($"Deserialize error for file <{FilePath}>", e, GetType());
|
||||
|
||||
return defaultData;
|
||||
}
|
||||
finally
|
||||
@@ -128,12 +132,12 @@ namespace Wox.Infrastructure.Storage
|
||||
}
|
||||
catch (SerializationException e)
|
||||
{
|
||||
Log.Exception($"|BinaryStorage.Save|serialize error for file <{FilePath}>", e);
|
||||
Log.Exception($"Serialize error for file <{FilePath}>", e, GetType());
|
||||
}
|
||||
}
|
||||
|
||||
_storageHelper.Close();
|
||||
Log.Info($"|BinaryStorage.Save|Saving cached data at <{FilePath}>");
|
||||
Log.Info($"Saving cached data at <{FilePath}>", GetType());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ namespace Wox.Infrastructure.Storage
|
||||
if (File.Exists(FilePath))
|
||||
{
|
||||
File.Delete(FilePath);
|
||||
Log.Info($"|JsonStorage.TryLoad|Deleting cached data at <{FilePath}>");
|
||||
Log.Info($"Deleting cached data at <{FilePath}>", GetType());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ namespace Wox.Infrastructure.Storage
|
||||
catch (JsonException e)
|
||||
{
|
||||
LoadDefault();
|
||||
Log.Exception($"|JsonStorage.Deserialize|Deserialize error for json <{FilePath}>", e);
|
||||
Log.Exception($"Deserialize error for json <{FilePath}>", e, GetType());
|
||||
}
|
||||
|
||||
if (_data == null)
|
||||
@@ -123,11 +123,12 @@ namespace Wox.Infrastructure.Storage
|
||||
string serialized = JsonConvert.SerializeObject(_data, Formatting.Indented);
|
||||
File.WriteAllText(FilePath, serialized);
|
||||
_storageHelper.Close();
|
||||
Log.Info($"|JsonStorage.Save|Saving cached data at <{FilePath}>");
|
||||
|
||||
Log.Info($"Saving cached data at <{FilePath}>", GetType());
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
Log.Error($"|JsonStorage.Save|Error in saving data at <{FilePath}>", e.Message);
|
||||
Log.Exception($"Error in saving data at <{FilePath}>", e, GetType());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace Wox.Infrastructure.Storage
|
||||
}
|
||||
catch (ArgumentException e)
|
||||
{
|
||||
Log.Info($"|LisRepository.Set| Trying to insert a duplicate item", e.Message);
|
||||
Log.Info($"Trying to insert a duplicate item {e.Message}", GetType());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ namespace Wox.Infrastructure.Storage
|
||||
{
|
||||
if (!_items.TryAdd(insertedItem.GetHashCode(), insertedItem))
|
||||
{
|
||||
Log.Error($"|ListRepository.Add| Item Already Exists <{insertedItem}>");
|
||||
Log.Error($"Item Already Exists <{insertedItem}>", GetType());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ namespace Wox.Infrastructure.Storage
|
||||
{
|
||||
if (!_items.TryRemove(removedItem.GetHashCode(), out _))
|
||||
{
|
||||
Log.Error($"|ListRepository.Remove| Item Not Found <{removedItem}>");
|
||||
Log.Error($"Item Not Found <{removedItem}>", GetType());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -89,7 +89,7 @@ namespace Wox.Infrastructure.UserSettings
|
||||
}
|
||||
catch (ArgumentException e)
|
||||
{
|
||||
Logger.Log.Exception(nameof(Settings), "Failed to load QuerySearchPrecisionString value from Settings file", e);
|
||||
Logger.Log.Exception("Failed to load QuerySearchPrecisionString value from Settings file", e, GetType());
|
||||
|
||||
QuerySearchPrecision = StringMatcher.SearchPrecisionScore.Regular;
|
||||
StringMatcher.Instance.UserSettingSearchPrecision = StringMatcher.SearchPrecisionScore.Regular;
|
||||
|
||||
Reference in New Issue
Block a user