59 lines
2.0 KiB
C#
59 lines
2.0 KiB
C#
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using DesktopBot.Engine;
|
|
using DesktopBot.Models;
|
|
|
|
namespace DesktopBot.ViewModels
|
|
{
|
|
/// <summary>
|
|
/// ViewModel per il Live Log
|
|
/// </summary>
|
|
public class LiveLogViewModel : BaseViewModel
|
|
{
|
|
private ObservableCollection<BotLogEntry> _logs = new ObservableCollection<BotLogEntry>();
|
|
|
|
public ObservableCollection<BotLogEntry> Logs
|
|
{
|
|
get => _logs;
|
|
set => SetProperty(ref _logs, value);
|
|
}
|
|
|
|
public ICommand ClearLogsCommand { get; }
|
|
public ICommand CopyLogsCommand { get; }
|
|
|
|
public LiveLogViewModel(AutomatedBotEngine botEngine)
|
|
{
|
|
ClearLogsCommand = new RelayCommand(_ => Logs.Clear());
|
|
CopyLogsCommand = new RelayCommand(_ =>
|
|
{
|
|
if (Logs.Count == 0) return;
|
|
var sb = new StringBuilder();
|
|
// I log sono inseriti in testa (più recente prima): li copiamo in ordine cronologico
|
|
foreach (var entry in Logs.Reverse())
|
|
sb.AppendLine(string.Format("[{0:HH:mm:ss dd/MM}] [{1}] {2}",
|
|
entry.Timestamp, entry.Level, entry.Message));
|
|
Clipboard.SetText(sb.ToString());
|
|
});
|
|
AddLog(new BotLogEntry(LogLevel.Info, "Sistema avviato - in attesa di configurazione"));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiunge un log entry in cima alla lista, sul UI thread (limiti parametrizzati)
|
|
/// </summary>
|
|
public void AddLog(BotLogEntry logEntry)
|
|
{
|
|
Application.Current?.Dispatcher.Invoke(() =>
|
|
{
|
|
Logs.Insert(0, logEntry);
|
|
// Limite massimo per il live log (default 10000)
|
|
int maxLiveLogEntries = 10000;
|
|
while (Logs.Count > maxLiveLogEntries)
|
|
Logs.RemoveAt(Logs.Count - 1);
|
|
});
|
|
}
|
|
}
|
|
}
|