39 lines
1.1 KiB
C#
39 lines
1.1 KiB
C#
using System.Collections.ObjectModel;
|
|
using System.IO;
|
|
|
|
namespace ReSync
|
|
{
|
|
public class Logger
|
|
{
|
|
private readonly string _logFilePath;
|
|
public ObservableCollection<LogEntry> Entries { get; } = new();
|
|
|
|
public Logger(string logFilePath)
|
|
{
|
|
_logFilePath = logFilePath;
|
|
Directory.CreateDirectory(Path.GetDirectoryName(logFilePath)!);
|
|
}
|
|
|
|
public void Log(string message, string level = "Info")
|
|
{
|
|
var entry = new LogEntry { Message = message, Level = level };
|
|
App.Current.Dispatcher.Invoke(() => Entries.Add(entry)); // update UI safely
|
|
|
|
var line = $"{entry.Timestamp:yyyy-MM-dd HH:mm:ss} [{entry.Level}] {entry.Message}";
|
|
File.AppendAllText(_logFilePath, line + Environment.NewLine);
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
Entries.Clear();
|
|
File.WriteAllText(_logFilePath, "");
|
|
}
|
|
}
|
|
|
|
public class LogEntry
|
|
{
|
|
public DateTime Timestamp { get; set; } = DateTime.Now;
|
|
public string Level { get; set; } = "Info";
|
|
public string Message { get; set; } = "";
|
|
}
|
|
} |