50 lines
1.3 KiB
C#
50 lines
1.3 KiB
C#
using SzCore;
|
|
using SzCore.Defaults;
|
|
|
|
namespace SzCli;
|
|
|
|
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($"[SZ LOG] {text}");
|
|
AppendLogFile($"{DateTime.UtcNow} : [SZ LOG] {text}");
|
|
}
|
|
|
|
public void LogError(string text)
|
|
{
|
|
Console.WriteLine($"[SZ ERR] {text}");
|
|
AppendLogFile($"{DateTime.UtcNow} : [SZ ERR] {text}");
|
|
}
|
|
|
|
public void LogWarning(string 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 (!LogToFile) return;
|
|
File.AppendAllText(LogFilePath, text + "\n");
|
|
}
|
|
} |