2021-10-25 10:56:00 +03:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
|
// 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.Diagnostics;
|
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.IO.Abstractions;
|
|
|
|
|
|
using System.Reflection;
|
2024-09-16 16:09:43 -04:00
|
|
|
|
|
2024-08-08 15:26:43 +01:00
|
|
|
|
using PowerToys.Interop;
|
2021-10-25 10:56:00 +03:00
|
|
|
|
|
2023-03-21 10:27:29 +01:00
|
|
|
|
namespace ManagedCommon
|
2021-10-25 10:56:00 +03:00
|
|
|
|
{
|
|
|
|
|
|
public static class Logger
|
|
|
|
|
|
{
|
|
|
|
|
|
private static readonly IFileSystem _fileSystem = new FileSystem();
|
|
|
|
|
|
private static readonly Assembly Assembly = Assembly.GetExecutingAssembly();
|
2023-03-21 10:27:29 +01:00
|
|
|
|
private static readonly string Version = FileVersionInfo.GetVersionInfo(Assembly.Location).ProductVersion;
|
2021-10-25 10:56:00 +03:00
|
|
|
|
|
|
|
|
|
|
private static readonly string Error = "Error";
|
|
|
|
|
|
private static readonly string Warning = "Warning";
|
|
|
|
|
|
private static readonly string Info = "Info";
|
|
|
|
|
|
private static readonly string Debug = "Debug";
|
|
|
|
|
|
private static readonly string TraceFlag = "Trace";
|
|
|
|
|
|
|
2023-03-21 10:27:29 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes the logger and sets the path for logging.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <example>InitializeLogger("\\FancyZones\\Editor\\Logs")</example>
|
|
|
|
|
|
/// <param name="applicationLogPath">The path to the log files folder.</param>
|
|
|
|
|
|
/// <param name="isLocalLow">If the process using Logger is a low-privilege process.</param>
|
|
|
|
|
|
public static void InitializeLogger(string applicationLogPath, bool isLocalLow = false)
|
2021-10-25 10:56:00 +03:00
|
|
|
|
{
|
2023-03-21 10:27:29 +01:00
|
|
|
|
if (isLocalLow)
|
2021-10-25 10:56:00 +03:00
|
|
|
|
{
|
2023-03-21 10:27:29 +01:00
|
|
|
|
applicationLogPath = Environment.GetEnvironmentVariable("userprofile") + "\\appdata\\LocalLow\\Microsoft\\PowerToys" + applicationLogPath + "\\" + Version;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
applicationLogPath = Constants.AppDataPath() + applicationLogPath + "\\" + Version;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!_fileSystem.Directory.Exists(applicationLogPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
_fileSystem.Directory.CreateDirectory(applicationLogPath);
|
2021-10-25 10:56:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-21 10:27:29 +01:00
|
|
|
|
var logFilePath = _fileSystem.Path.Combine(applicationLogPath, "Log_" + DateTime.Now.ToString(@"yyyy-MM-dd", CultureInfo.InvariantCulture) + ".txt");
|
2021-10-25 10:56:00 +03:00
|
|
|
|
|
|
|
|
|
|
Trace.Listeners.Add(new TextWriterTraceListener(logFilePath));
|
|
|
|
|
|
|
|
|
|
|
|
Trace.AutoFlush = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void LogError(string message)
|
|
|
|
|
|
{
|
|
|
|
|
|
Log(message, Error);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void LogError(string message, Exception ex)
|
|
|
|
|
|
{
|
2024-10-18 11:11:18 +02:00
|
|
|
|
if (ex == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
LogError(message);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var exMessage =
|
|
|
|
|
|
message + Environment.NewLine +
|
|
|
|
|
|
ex.GetType() + ": " + ex.Message + Environment.NewLine;
|
|
|
|
|
|
|
|
|
|
|
|
if (ex.InnerException != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
exMessage +=
|
|
|
|
|
|
"Inner exception: " + Environment.NewLine +
|
|
|
|
|
|
ex.InnerException.GetType() + ": " + ex.InnerException.Message + Environment.NewLine;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
exMessage +=
|
|
|
|
|
|
"Stack trace: " + Environment.NewLine +
|
|
|
|
|
|
ex.StackTrace;
|
|
|
|
|
|
|
|
|
|
|
|
Log(exMessage, Error);
|
|
|
|
|
|
}
|
2021-10-25 10:56:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void LogWarning(string message)
|
|
|
|
|
|
{
|
|
|
|
|
|
Log(message, Warning);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void LogInfo(string message)
|
|
|
|
|
|
{
|
|
|
|
|
|
Log(message, Info);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void LogDebug(string message)
|
|
|
|
|
|
{
|
|
|
|
|
|
Log(message, Debug);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void LogTrace()
|
|
|
|
|
|
{
|
|
|
|
|
|
Log(string.Empty, TraceFlag);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void Log(string message, string type)
|
|
|
|
|
|
{
|
|
|
|
|
|
Trace.WriteLine("[" + DateTime.Now.TimeOfDay + "] [" + type + "] " + GetCallerInfo());
|
|
|
|
|
|
Trace.Indent();
|
|
|
|
|
|
if (message != string.Empty)
|
|
|
|
|
|
{
|
|
|
|
|
|
Trace.WriteLine(message);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Trace.Unindent();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static string GetCallerInfo()
|
|
|
|
|
|
{
|
2023-03-21 10:27:29 +01:00
|
|
|
|
StackTrace stackTrace = new();
|
2021-10-25 10:56:00 +03:00
|
|
|
|
|
|
|
|
|
|
var methodName = stackTrace.GetFrame(3)?.GetMethod();
|
|
|
|
|
|
var className = methodName?.DeclaringType.Name;
|
2021-10-25 13:05:44 +03:00
|
|
|
|
return className + "::" + methodName?.Name;
|
2021-10-25 10:56:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|