mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-04 18:26:39 +02:00
Centralize c# logger (#22760)
* Initial commit * Changed some loggers (WIP) * ColorPicker * Add version to all logs * FancyZones * push * PowerOCR and Measuretool * Settings * Hosts + Fix settings * Fix some using statements * FileLocksmith * Fix awake * Fixed Hosts logger * Fix spelling * Remove added submodule * Fiy FileLocksmith and PowerAccent * Fix PowerAccent * Test * Changed logger locic and added ColorPicker * Fixed package * Add documentation * Add locallow capability to Logger and add FancyZones * Fixed FancyZones and added FileLocksmith * Add Hosts * Fixed spelling mistakes * Add MeasureTool * Add MouseJump * Add PowerOCR * Add PowerAccent * Add monaco * Add Settings * Fixed Monaco * Update installer * Update doc/devdocs/logging.md Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com> * Update doc/devdocs/logging.md Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com> * Update doc/devdocs/logging.md Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com> * Update logging.md * Fix unneccesairy includes. --------- Co-authored-by: Dustin L. Howett <dustin@howett.net> Co-authored-by: Stefan Markovic <stefan@janeasystems.com> Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com>
This commit is contained in:
@@ -8,7 +8,6 @@ using System.Threading;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using Common.UI;
|
||||
using FancyZonesEditor.Logs;
|
||||
using FancyZonesEditor.Utils;
|
||||
using ManagedCommon;
|
||||
|
||||
@@ -55,6 +54,8 @@ namespace FancyZonesEditor
|
||||
|
||||
public App()
|
||||
{
|
||||
Logger.InitializeLogger("\\FancyZones\\Editor\\Logs");
|
||||
|
||||
// DebugModeCheck();
|
||||
NativeThreadCTS = new CancellationTokenSource();
|
||||
FancyZonesEditorIO = new FancyZonesEditorIO();
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using FancyZonesEditor.Logs;
|
||||
using FancyZonesEditor.Models;
|
||||
using ManagedCommon;
|
||||
|
||||
namespace FancyZonesEditor
|
||||
{
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
|
||||
using System;
|
||||
using System.Windows;
|
||||
using FancyZonesEditor.Logs;
|
||||
using FancyZonesEditor.Models;
|
||||
using ManagedCommon;
|
||||
|
||||
namespace FancyZonesEditor
|
||||
{
|
||||
|
||||
@@ -6,8 +6,8 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows.Controls;
|
||||
using FancyZonesEditor.Logs;
|
||||
using FancyZonesEditor.Models;
|
||||
using ManagedCommon;
|
||||
|
||||
namespace FancyZonesEditor
|
||||
{
|
||||
|
||||
@@ -10,8 +10,8 @@ using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Input;
|
||||
using FancyZonesEditor.Logs;
|
||||
using FancyZonesEditor.Models;
|
||||
using ManagedCommon;
|
||||
|
||||
namespace FancyZonesEditor
|
||||
{
|
||||
|
||||
@@ -7,8 +7,8 @@ using System.Collections.Generic;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
using FancyZonesEditor.Logs;
|
||||
using FancyZonesEditor.Models;
|
||||
using ManagedCommon;
|
||||
|
||||
namespace FancyZonesEditor
|
||||
{
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
// 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;
|
||||
using System.IO.Abstractions;
|
||||
using System.Reflection;
|
||||
using interop;
|
||||
|
||||
namespace FancyZonesEditor.Logs
|
||||
{
|
||||
public static class Logger
|
||||
{
|
||||
private static readonly IFileSystem _fileSystem = new FileSystem();
|
||||
private static readonly Assembly Assembly = Assembly.GetExecutingAssembly();
|
||||
public static readonly string Version = FileVersionInfo.GetVersionInfo(Assembly.Location).ProductVersion;
|
||||
private static readonly string ApplicationLogPath = Path.Combine(Constants.AppDataPath(), "FancyZones\\Editor\\Logs\\", Version);
|
||||
|
||||
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";
|
||||
|
||||
static Logger()
|
||||
{
|
||||
if (!_fileSystem.Directory.Exists(ApplicationLogPath))
|
||||
{
|
||||
_fileSystem.Directory.CreateDirectory(ApplicationLogPath);
|
||||
}
|
||||
|
||||
// Using InvariantCulture since this is used for a log file name
|
||||
var logFilePath = _fileSystem.Path.Combine(ApplicationLogPath, "Log_" + DateTime.Now.ToString(@"yyyy-MM-dd", CultureInfo.InvariantCulture) + ".txt");
|
||||
|
||||
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)
|
||||
{
|
||||
Log(
|
||||
message + Environment.NewLine +
|
||||
ex?.Message + Environment.NewLine +
|
||||
"Inner exception: " + Environment.NewLine +
|
||||
ex?.InnerException?.Message + Environment.NewLine +
|
||||
"Stack trace: " + Environment.NewLine +
|
||||
ex?.StackTrace,
|
||||
Error);
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
StackTrace stackTrace = new StackTrace();
|
||||
|
||||
var methodName = stackTrace.GetFrame(3)?.GetMethod();
|
||||
var className = methodName?.DeclaringType.Name;
|
||||
return className + "::" + methodName?.Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,9 +11,9 @@ using System.Windows.Automation.Peers;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using Common.UI;
|
||||
using FancyZonesEditor.Logs;
|
||||
using FancyZonesEditor.Models;
|
||||
using FancyZonesEditor.Utils;
|
||||
using ManagedCommon;
|
||||
using ModernWpf.Controls;
|
||||
|
||||
namespace FancyZonesEditor
|
||||
|
||||
@@ -6,8 +6,8 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using FancyZonesEditor.Logs;
|
||||
using FancyZonesEditor.Models;
|
||||
using ManagedCommon;
|
||||
|
||||
namespace FancyZonesEditor
|
||||
{
|
||||
|
||||
@@ -12,8 +12,8 @@ using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using FancyZonesEditor.Logs;
|
||||
using FancyZonesEditor.Models;
|
||||
using ManagedCommon;
|
||||
|
||||
namespace FancyZonesEditor.Utils
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user