Converting to Async

This commit is contained in:
2026-01-29 20:22:30 -06:00
parent 0dd597ee4e
commit 30236924a0
5 changed files with 148 additions and 124 deletions

View File

@@ -8,38 +8,43 @@ public class Logger : ISzLogger
public bool LogToFile { get; set; } = true;
public string LogFilePath { get; set; }
public int LogFileMaxLines { get; set; } = 100;
public readonly ISzFileManager FileManager;
public Logger(ISzFileManager fileManager)
{
FileManager = fileManager;
LogFilePath = Path.Combine(FileManager.DataPath, "log.txt");
AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) =>
{
LogError($"[EXCEPTION] {eventArgs.Exception}");
};
}
public void Log(string text)
{
Console.WriteLine($"{DateTime.UtcNow} : [SZ LOG] {text}");
if (LogToFile) AppendLogFile($"{DateTime.UtcNow} : [SZ LOG] {text}");
Console.WriteLine($"[SZ LOG] {text}");
AppendLogFile($"{DateTime.UtcNow} : [SZ LOG] {text}");
}
public void LogError(string text)
{
Console.WriteLine($"{DateTime.UtcNow} : [SZ ERR] {text}");
if (LogToFile) AppendLogFile($"{DateTime.UtcNow} : [SZ ERR] {text}");
Console.WriteLine($"[SZ ERR] {text}");
AppendLogFile($"{DateTime.UtcNow} : [SZ ERR] {text}");
}
public void LogWarning(string text)
{
Console.WriteLine($"{DateTime.UtcNow} : [SZ WARN] {text}");
if (LogToFile) AppendLogFile($"{DateTime.UtcNow} : [SZ WARN] {text}");
Console.WriteLine($"[SZ WARN] {text}");
AppendLogFile($"{DateTime.UtcNow} : [SZ WARN] {text}");
}
private void AppendLogFile(string text)
{
// TODO: Make sure log file adheres to LogFileMaxLines
if (!File.Exists(LogFilePath))
{
LogFilePath = Path.Combine(SZ.LocalFileManager.DataPath, "log.txt");
AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) =>
{
LogError($"[EXCEPTION] {eventArgs.Exception}");
};
}
if (!LogToFile) return;
File.AppendAllText(LogFilePath, text + "\n");
}
}