2020-07-07 07:15:45 +12:00
|
|
|
|
// <copyright file="Log.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
2020-03-17 02:45:19 +13:00
|
|
|
|
|
2020-03-28 12:42:32 +13:00
|
|
|
|
namespace SystemTrayMenu.Utilities
|
2020-03-17 02:45:19 +13:00
|
|
|
|
{
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using System;
|
2020-09-27 01:54:24 +12:00
|
|
|
|
using System.Collections.Generic;
|
2021-11-30 08:22:16 +13:00
|
|
|
|
using System.ComponentModel;
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Reflection;
|
2021-10-06 09:23:41 +13:00
|
|
|
|
using System.Threading;
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using System.Windows.Forms;
|
2020-07-08 03:05:19 +12:00
|
|
|
|
using Clearcove.Logging;
|
2020-07-07 09:37:55 +12:00
|
|
|
|
|
2020-03-17 02:45:19 +13:00
|
|
|
|
internal static class Log
|
|
|
|
|
{
|
2022-02-05 23:28:19 +13:00
|
|
|
|
private const string LogfileLastExtension = "_last";
|
2021-11-17 12:13:46 +13:00
|
|
|
|
private static readonly Logger LogValue = new(string.Empty);
|
|
|
|
|
private static readonly List<string> Warnings = new();
|
|
|
|
|
private static readonly List<string> Infos = new();
|
2020-07-07 09:37:55 +12:00
|
|
|
|
|
2020-03-17 02:45:19 +13:00
|
|
|
|
internal static void Initialize()
|
|
|
|
|
{
|
2022-06-06 01:27:35 +12:00
|
|
|
|
bool warnFailedToSaveLogFile = false;
|
|
|
|
|
Exception exceptionWarnFailedToSaveLogFile = new();
|
|
|
|
|
if (Properties.Settings.Default.SaveLogFileInApplicationDirectory)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string fileNameToCheckWriteAccess = "CheckWriteAccess";
|
|
|
|
|
File.WriteAllText(fileNameToCheckWriteAccess, fileNameToCheckWriteAccess);
|
|
|
|
|
File.Delete(fileNameToCheckWriteAccess);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Properties.Settings.Default.SaveLogFileInApplicationDirectory = false;
|
|
|
|
|
warnFailedToSaveLogFile = true;
|
|
|
|
|
exceptionWarnFailedToSaveLogFile = ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool warnCanNotClearLogfile = false;
|
|
|
|
|
Exception exceptionWarnCanNotClearLogfile = new();
|
2022-02-05 23:28:19 +13:00
|
|
|
|
string fileNamePath = GetLogFilePath();
|
2022-02-06 00:47:27 +13:00
|
|
|
|
FileInfo fileInfo = new(fileNamePath);
|
2022-06-06 01:27:35 +12:00
|
|
|
|
string fileNamePathLast = string.Empty;
|
2022-02-07 19:26:59 +13:00
|
|
|
|
if (fileInfo.Exists && fileInfo.Length > 2000000)
|
2022-02-05 23:28:19 +13:00
|
|
|
|
{
|
2022-06-06 01:27:35 +12:00
|
|
|
|
fileNamePathLast = GetLogFilePath(LogfileLastExtension);
|
2022-02-05 23:28:19 +13:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
File.Delete(fileNamePathLast);
|
|
|
|
|
File.Move(fileNamePath, fileNamePathLast);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2022-06-06 01:27:35 +12:00
|
|
|
|
warnCanNotClearLogfile = true;
|
|
|
|
|
exceptionWarnCanNotClearLogfile = ex;
|
2022-02-05 23:28:19 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.Start(fileInfo);
|
2022-06-06 01:27:35 +12:00
|
|
|
|
|
|
|
|
|
if (warnFailedToSaveLogFile)
|
|
|
|
|
{
|
|
|
|
|
Warn($"Failed to save log file in application folder {GetLogFilePath()}", exceptionWarnFailedToSaveLogFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (warnCanNotClearLogfile)
|
|
|
|
|
{
|
|
|
|
|
Warn($"Can not clear logfile:'{fileNamePathLast}'", exceptionWarnCanNotClearLogfile);
|
|
|
|
|
}
|
2020-03-17 02:45:19 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void Info(string message)
|
|
|
|
|
{
|
2020-10-25 09:31:56 +13:00
|
|
|
|
if (!Infos.Contains(message))
|
2020-09-27 01:54:24 +12:00
|
|
|
|
{
|
2020-10-25 09:31:56 +13:00
|
|
|
|
Infos.Add(message);
|
2020-09-27 03:48:37 +13:00
|
|
|
|
LogValue.Info(message);
|
2020-09-27 01:54:24 +12:00
|
|
|
|
}
|
2020-03-17 02:45:19 +13:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-30 06:26:45 +13:00
|
|
|
|
internal static void Warn(string message, Exception ex)
|
|
|
|
|
{
|
2020-09-27 01:54:24 +12:00
|
|
|
|
string warning = $"{message} {ex.ToString().Replace(Environment.NewLine, " ", StringComparison.InvariantCulture)}";
|
2020-10-25 09:31:56 +13:00
|
|
|
|
if (!Warnings.Contains(warning))
|
2020-09-27 01:54:24 +12:00
|
|
|
|
{
|
2020-10-25 09:31:56 +13:00
|
|
|
|
Warnings.Add(warning);
|
2020-09-27 03:48:37 +13:00
|
|
|
|
LogValue.Warn(warning);
|
2020-09-27 01:54:24 +12:00
|
|
|
|
}
|
2020-03-30 06:26:45 +13:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 02:45:19 +13:00
|
|
|
|
internal static void Error(string message, Exception ex)
|
|
|
|
|
{
|
2020-07-07 09:37:55 +12:00
|
|
|
|
LogValue.Error($"{message}{Environment.NewLine}" +
|
2020-07-07 07:15:45 +12:00
|
|
|
|
$"{ex}");
|
2020-03-17 02:45:19 +13:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-05 23:28:19 +13:00
|
|
|
|
internal static string GetLogFilePath(string backup = "")
|
2020-03-17 02:45:19 +13:00
|
|
|
|
{
|
2022-06-06 01:27:35 +12:00
|
|
|
|
string logFilePath = string.Empty;
|
|
|
|
|
if (!Properties.Settings.Default.SaveLogFileInApplicationDirectory)
|
|
|
|
|
{
|
|
|
|
|
logFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), $"SystemTrayMenu");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Path.Combine(logFilePath, $"log-{Environment.MachineName}{backup}.txt");
|
2020-03-17 02:45:19 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void OpenLogFile()
|
|
|
|
|
{
|
2022-02-05 23:28:19 +13:00
|
|
|
|
string lastLogfile = GetLogFilePath(LogfileLastExtension);
|
|
|
|
|
if (File.Exists(lastLogfile))
|
|
|
|
|
{
|
|
|
|
|
ProcessStart(lastLogfile);
|
|
|
|
|
}
|
2022-06-06 01:27:35 +12:00
|
|
|
|
|
|
|
|
|
ProcessStart(GetLogFilePath());
|
2020-03-17 02:45:19 +13:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 09:05:52 +13:00
|
|
|
|
internal static void WriteApplicationRuns()
|
2020-03-17 02:45:19 +13:00
|
|
|
|
{
|
|
|
|
|
Assembly assembly = Assembly.GetExecutingAssembly();
|
2020-07-07 09:37:55 +12:00
|
|
|
|
LogValue.Info($"Application Start " +
|
2020-03-17 03:57:51 +13:00
|
|
|
|
assembly.ManifestModule.Name + " | " +
|
|
|
|
|
assembly.GetName().Version.ToString() + " | " +
|
|
|
|
|
$"ScalingFactor={Scaling.Factor}");
|
2020-03-17 02:45:19 +13:00
|
|
|
|
}
|
2020-03-17 09:05:52 +13:00
|
|
|
|
|
|
|
|
|
internal static void Close()
|
|
|
|
|
{
|
2022-06-06 01:27:35 +12:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Logger.ShutDown();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Properties.Settings.Default.SaveLogFileInApplicationDirectory = false;
|
|
|
|
|
Warn($"Failed to save log file in application folder {GetLogFilePath()}", ex);
|
|
|
|
|
}
|
2020-03-17 09:05:52 +13:00
|
|
|
|
}
|
2020-03-30 06:26:45 +13:00
|
|
|
|
|
2021-05-03 01:01:31 +12:00
|
|
|
|
internal static void ProcessStart(
|
|
|
|
|
string fileName,
|
|
|
|
|
string arguments = "",
|
|
|
|
|
bool doubleQuoteArg = false,
|
|
|
|
|
string workingDirectory = "",
|
2022-06-26 20:56:47 +12:00
|
|
|
|
bool createNoWindow = false,
|
|
|
|
|
string resolvedPath = "")
|
2020-03-30 06:26:45 +13:00
|
|
|
|
{
|
2021-05-03 01:01:31 +12:00
|
|
|
|
if (doubleQuoteArg && !string.IsNullOrEmpty(arguments))
|
2020-07-16 22:42:00 +12:00
|
|
|
|
{
|
|
|
|
|
arguments = "\"" + arguments + "\"";
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-09 03:54:19 +12:00
|
|
|
|
try
|
2020-03-30 06:26:45 +13:00
|
|
|
|
{
|
2021-11-17 12:13:46 +13:00
|
|
|
|
using Process p = new()
|
2020-07-09 03:54:19 +12:00
|
|
|
|
{
|
|
|
|
|
StartInfo = new ProcessStartInfo(fileName)
|
|
|
|
|
{
|
2021-05-03 01:01:31 +12:00
|
|
|
|
FileName = fileName,
|
2020-07-09 03:54:19 +12:00
|
|
|
|
Arguments = arguments,
|
2021-05-03 01:01:31 +12:00
|
|
|
|
WorkingDirectory = workingDirectory,
|
|
|
|
|
CreateNoWindow = createNoWindow,
|
2020-07-09 03:54:19 +12:00
|
|
|
|
UseShellExecute = true,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
p.Start();
|
2020-03-30 06:26:45 +13:00
|
|
|
|
}
|
2021-11-30 08:22:16 +13:00
|
|
|
|
catch (Win32Exception ex)
|
2020-03-30 06:26:45 +13:00
|
|
|
|
{
|
2021-10-06 09:23:41 +13:00
|
|
|
|
Warn($"fileName:'{fileName}' arguments:'{arguments}'", ex);
|
2021-11-30 08:22:16 +13:00
|
|
|
|
|
2022-06-26 20:56:47 +12:00
|
|
|
|
if ((ex.NativeErrorCode == 2 || ex.NativeErrorCode == 1223) &&
|
|
|
|
|
(string.IsNullOrEmpty(resolvedPath) || !File.Exists(resolvedPath)))
|
2020-03-30 06:26:45 +13:00
|
|
|
|
{
|
2021-10-06 09:23:41 +13:00
|
|
|
|
new Thread(ShowProblemWithShortcut).Start();
|
2021-10-11 04:33:54 +13:00
|
|
|
|
static void ShowProblemWithShortcut()
|
2021-10-06 09:23:41 +13:00
|
|
|
|
{
|
|
|
|
|
_ = MessageBox.Show(
|
|
|
|
|
Translator.GetText("The item that this shortcut refers to has been changed or moved, so this shortcut will no longer work properly."),
|
2022-05-30 02:41:23 +12:00
|
|
|
|
Translator.GetText("Problem with shortcut link"),
|
2021-10-06 09:23:41 +13:00
|
|
|
|
MessageBoxButtons.OK,
|
|
|
|
|
MessageBoxIcon.Warning);
|
|
|
|
|
}
|
2020-03-30 06:26:45 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-30 08:22:16 +13:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Warn($"fileName:'{fileName}' arguments:'{arguments}'", ex);
|
|
|
|
|
}
|
2020-03-30 06:26:45 +13:00
|
|
|
|
}
|
2020-03-17 02:45:19 +13:00
|
|
|
|
}
|
|
|
|
|
}
|