diff --git a/Examples/Nodify.Calculator/LogPanelViewModel.cs b/Examples/Nodify.Calculator/LogPanelViewModel.cs index fd702f3..9c61015 100644 --- a/Examples/Nodify.Calculator/LogPanelViewModel.cs +++ b/Examples/Nodify.Calculator/LogPanelViewModel.cs @@ -1,6 +1,9 @@ using System; using System.Collections.ObjectModel; +using System.IO; +using System.Text; using System.Windows.Media; +using Microsoft.Win32; namespace Nodify.Calculator { @@ -41,11 +44,50 @@ namespace Nodify.Calculator public INodifyCommand ClearLogsCommand { get; } public INodifyCommand ClosePanelCommand { get; } + public INodifyCommand ExportLogsCommand { get; } public LogPanelViewModel() { ClearLogsCommand = new DelegateCommand(() => LogEntries.Clear()); ClosePanelCommand = new DelegateCommand(() => IsOpen = false); + ExportLogsCommand = new DelegateCommand(ExportLogs); + } + + private void ExportLogs() + { + if (LogEntries.Count == 0) + { + System.Windows.MessageBox.Show("There are no logs to export.", "Export Logs", + System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Information); + return; + } + + var dialog = new SaveFileDialog + { + Title = "Export Logs", + Filter = "Log file (*.log)|*.log|Text file (*.txt)|*.txt|All files (*.*)|*.*", + FileName = $"APIVisualExecutor_Logs_{DateTime.Now:yyyyMMdd_HHmmss}.log" + }; + + if (dialog.ShowDialog() != true) + return; + + try + { + var sb = new StringBuilder(); + foreach (var entry in LogEntries) + { + sb.Append('[').Append(entry.Timestamp).Append("] [") + .Append(entry.Level).Append("] ") + .AppendLine(entry.Message); + } + File.WriteAllText(dialog.FileName, sb.ToString(), Encoding.UTF8); + } + catch (Exception ex) + { + System.Windows.MessageBox.Show($"Failed to export logs: {ex.Message}", "Export Logs", + System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); + } } public void WriteLog(string message, logType logtype = logType.Information) diff --git a/Examples/Nodify.Calculator/MainWindow.xaml b/Examples/Nodify.Calculator/MainWindow.xaml index 2e6ebd1..43f95ed 100644 --- a/Examples/Nodify.Calculator/MainWindow.xaml +++ b/Examples/Nodify.Calculator/MainWindow.xaml @@ -135,6 +135,10 @@