Projektdateien hinzufügen.
This commit is contained in:
39
BackEnd/Logger.cs
Normal file
39
BackEnd/Logger.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
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; } = "";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user