SystemTrayMenu/Utilities/Log.cs
2020-06-05 18:30:26 +02:00

107 lines
3.1 KiB
C#

using Clearcove.Logging;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
namespace SystemTrayMenu.Utilities
{
internal static class Log
{
private static readonly Logger log = new Logger("");
internal static void Initialize()
{
Logger.Start(new FileInfo(GetLogFilePath()));
}
internal static void Info(string message)
{
log.Info(message);
}
internal static void Warn(string message, Exception ex)
{
log.Warn($"{message}{Environment.NewLine}{ex}");
}
//internal static void Debug(string message)
//{
// log.Debug($"{message}{Environment.NewLine}" +
// $"{Environment.StackTrace.ToString()}");
//}
internal static void Error(string message, Exception ex)
{
log.Error($"{message}{Environment.NewLine}" +
$"{ex.ToString()}");
}
internal static string GetLogFilePath()
{
return Path.Combine(Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location),
$"log-{Environment.MachineName}.txt");
}
internal static void OpenLogFile()
{
ProcessStart(GetLogFilePath());
}
internal static void WriteApplicationRuns()
{
Assembly assembly = Assembly.GetExecutingAssembly();
log.Info($"Application Start " +
assembly.ManifestModule.Name + " | " +
assembly.GetName().Version.ToString() + " | " +
$"ScalingFactor={Scaling.Factor}");
}
internal static void Close()
{
Logger.ShutDown();
}
internal static void ProcessStart(string fileName, string arguments = null)
{
if (!string.IsNullOrEmpty(arguments) &&
!Directory.Exists(arguments))
{
Exception ex = new DirectoryNotFoundException();
Warn($"path:'{arguments}'", ex);
MessageBox.Show(ex.Message);
}
else
{
try
{
using (Process p = new Process())
{
p.StartInfo = new ProcessStartInfo(fileName)
{
Arguments = arguments,
UseShellExecute = true
};
p.Start();
};
}
catch (Exception ex)
{
if (ex is FileNotFoundException ||
ex is Win32Exception)
{
Warn($"fileName:'{fileName}' arguments:'{arguments}'", ex);
MessageBox.Show(ex.Message);
}
else
{
throw;
}
}
}
}
}
}