71 lines
1.7 KiB
C#
Raw Normal View History

2024-06-11 19:18:48 -04:00
namespace AssemblyRemapper.Utils;
internal static class Logger
{
static Logger()
{
if (File.Exists(_logPath))
{
File.Delete(_logPath);
File.Create(_logPath).Close();
}
}
private static string _logPath = Path.Combine(AppContext.BaseDirectory, "Data", "Log.log");
2024-06-13 07:45:40 -04:00
public static void ClearLog()
{
if (File.Exists(_logPath))
{
File.Delete(_logPath);
File.Create(_logPath).Close();
}
}
public static void Log(object message, ConsoleColor color = ConsoleColor.Gray, bool silent = false)
2024-06-11 19:18:48 -04:00
{
2024-06-12 18:59:08 -04:00
if (silent)
{
WriteToDisk(message);
return;
}
2024-06-11 19:18:48 -04:00
Console.ForegroundColor = color;
Console.WriteLine(message);
Console.ResetColor();
2024-06-12 18:59:08 -04:00
WriteToDisk(message);
}
2024-06-11 19:18:48 -04:00
2024-06-13 07:45:40 -04:00
public static void LogDebug(object message, ConsoleColor color = ConsoleColor.Gray, bool silent = false)
2024-06-12 18:59:08 -04:00
{
if (silent)
{
WriteToDisk(message);
return;
}
2024-06-13 17:55:06 -04:00
if (DataProvider.Settings.AppSettings.Debug)
2024-06-12 18:59:08 -04:00
{
Console.ForegroundColor = color;
Console.WriteLine(message);
Console.ResetColor();
WriteToDisk(message);
}
}
2024-06-13 07:45:40 -04:00
private static void WriteToDisk(object message)
2024-06-12 18:59:08 -04:00
{
2024-06-11 19:18:48 -04:00
try
{
using (StreamWriter sw = File.AppendText(_logPath))
{
sw.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - {message}");
}
}
catch (IOException ex)
{
// Handle potential file writing errors gracefully
Console.WriteLine($"Error logging: {ex.Message}");
}
}
}