64 lines
1.5 KiB
C#
64 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.Windows;
|
|
|
|
namespace S7Explorer.Services
|
|
{
|
|
public class LogService
|
|
{
|
|
private static LogService? _instance;
|
|
private ObservableCollection<LogEntry> _logs = new();
|
|
|
|
// Singleton instance
|
|
public static LogService Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null)
|
|
_instance = new LogService();
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
public ObservableCollection<LogEntry> Logs => _logs;
|
|
|
|
public void LogInfo(string message)
|
|
{
|
|
AddLog(LogLevel.Info, message);
|
|
}
|
|
|
|
public void LogWarning(string message)
|
|
{
|
|
AddLog(LogLevel.Warning, message);
|
|
}
|
|
|
|
public void LogError(string message)
|
|
{
|
|
AddLog(LogLevel.Error, message);
|
|
}
|
|
|
|
public void LogError(Exception ex)
|
|
{
|
|
AddLog(LogLevel.Error, $"Error: {ex.Message}");
|
|
AddLog(LogLevel.Debug, $"Stack trace: {ex.StackTrace}");
|
|
}
|
|
|
|
public void LogDebug(string message)
|
|
{
|
|
AddLog(LogLevel.Debug, message);
|
|
}
|
|
|
|
private void AddLog(LogLevel level, string message)
|
|
{
|
|
Application.Current.Dispatcher.Invoke(() =>
|
|
{
|
|
_logs.Add(new LogEntry
|
|
{
|
|
Timestamp = DateTime.Now,
|
|
Level = level,
|
|
Message = message
|
|
});
|
|
});
|
|
}
|
|
}
|
|
} |