Added a logger interface

This commit is contained in:
2026-01-23 23:39:57 -06:00
parent b500b71e62
commit f78f8fdf57
4 changed files with 52 additions and 11 deletions

27
SzCli/Logger.cs Normal file
View File

@@ -0,0 +1,27 @@
using SzLib;
namespace SzCli;
public class Logger(ISzFileManager fileManager) : ISzLogger
{
public bool LogToFile { get; set; } = true;
public string LogFilePath { get; set; } = Path.Combine(fileManager.DataPath, "log.txt");
public void Log(string text)
{
Console.WriteLine($"{DateTime.UtcNow} : [SZ LOG] {text}");
if (LogToFile) File.AppendAllText(LogFilePath, $"{DateTime.UtcNow} : [SZ LOG] {text}\n");
}
public void LogError(string text)
{
Console.WriteLine($"{DateTime.UtcNow} : [SZ ERR] {text}");
if (LogToFile) File.AppendAllText(LogFilePath, $"{DateTime.UtcNow} : [SZ ERR] {text}\n");
}
public void LogWarning(string text)
{
Console.WriteLine($"{DateTime.UtcNow} : [SZ WARN] {text}");
if (LogToFile) File.AppendAllText(LogFilePath, $"{DateTime.UtcNow} : [SZ WARN] {text}\n");
}
}

View File

@@ -5,22 +5,25 @@ namespace SzCli;
public class Program
{
private static readonly ISzFileManager _fileManager = new LocalFileManager();
private static readonly ISzLogger _logger = new Logger(_fileManager);
public static void Main(string[] args)
{
var parser = new SzParser(new LocalFileManager());
var parser = new SzParser(_fileManager, _logger);
Console.WriteLine("Creating test dataset...");
_logger.Log("Creating test dataset...");
var dataset = Test.CreateTestDataset();
Console.WriteLine($"Successfully created test dataset with id {dataset.Id}");
_logger.Log($"Successfully created test dataset with id {dataset.Id}");
Console.WriteLine("Attempting to save dataset...");
_logger.Log("Attempting to save dataset...");
var saved = parser.SaveDataset(dataset);
Console.WriteLine($"Successfully saved dataset? {saved}");
_logger.Log($"Successfully saved dataset? {saved}");
Console.WriteLine("Attempting to load dataset...");
_logger.Log("Attempting to load dataset...");
var loadedDataset = parser.LoadDataset(dataset.Id);
Console.WriteLine($"Successfully loaded dataset? {loadedDataset != null}. Id is {loadedDataset?.Id}");
_logger.Log($"Successfully loaded dataset? {loadedDataset != null}. Id is {loadedDataset?.Id}");
Console.WriteLine(loadedDataset?.DataObjects["test"].Fields["value"].Value);
_logger.Log(loadedDataset?.DataObjects["test"].Fields["value"].Value);
}
}

10
SzLib/ISzLogger.cs Normal file
View File

@@ -0,0 +1,10 @@
namespace SzLib;
public interface ISzLogger
{
public bool LogToFile { get; set; }
public string LogFilePath { get; set; }
public void Log(string text);
public void LogError(string text);
public void LogWarning(string text);
}

View File

@@ -3,7 +3,7 @@ using SzLib.DataObjects;
namespace SzLib;
public class SzParser(ISzFileManager szFileManager)
public class SzParser(ISzFileManager szFileManager, ISzLogger szLogger)
{
private readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true };
@@ -29,7 +29,8 @@ public class SzParser(ISzFileManager szFileManager)
}
catch (Exception e)
{
throw new Exception("Could not deserialize JSON to type SzDataset: " + e.Message, e);
szLogger.LogError("Could not deserialize JSON to type SzDataset: " + e.Message);
return null;
}
}
@@ -42,7 +43,7 @@ public class SzParser(ISzFileManager szFileManager)
}
catch (Exception e)
{
Console.WriteLine("Error saving dataset: " + e.Message);
szLogger.LogError("Error saving dataset: " + e.Message);
return false;
}
}