Skip to content

Export Functionality #137

@boexler

Description

@boexler

Export Functionality - Implementation Plan

Overview

Implement export functionality that allows users to export filtered log entries to a *.log file. The export will be accessible via a menu item that is only enabled when a tab is selected.

Implementation Steps

1. Create Export Models

  • File: app/Sentinel.NLogViewer.App/Models/ExportParameter.cs
    • Create ExportParameter class with:
      • FilePath (string) - target file path
      • Format (ExportFormat enum) - export format type
    • Create ExportFormat enum with Log as default value (extensible for future formats)

2. Add Export Command to NLogViewer

  • File: ui/Sentinel.NLogViewer.Wpf/NLogViewer.xaml.cs
    • Add ExportCommand dependency property (ICommand)
    • Add method GetFilteredLogEntries() that returns IEnumerable<LogEventInfo> from LogEvents.View (filtered collection)
    • Add method ExportLogs(ExportParameter parameter) that:
      • Gets filtered log entries via GetFilteredLogEntries()
      • Writes entries to file in NLog format (using TimeStampResolver, LoggerNameResolver, MessageResolver)
      • Format: [Timestamp] [Level] LoggerName: Message (with exception if present)
    • Initialize ExportCommand in constructor as RelayCommand<ExportParameter>(ExportLogs)

3. Add Export Command to MainViewModel

  • File: app/Sentinel.NLogViewer.App/ViewModels/MainViewModel.cs
    • Add ExportLogsCommand property (ICommand)
    • Implement ExportLogs() method that:
      • Checks if SelectedTab != null
      • Opens SaveFileDialog with filter "Log Files (*.log)|*.log"
      • Finds NLogViewer instance from selected tab's visual tree (using helper method)
      • Creates ExportParameter with file path and ExportFormat.Log
      • Calls NLogViewer.ExportCommand.Execute(exportParameter)
    • Add helper method FindNLogViewerInTab() to locate NLogViewer instance
    • Initialize command in constructor: ExportLogsCommand = new RelayCommand(ExportLogs, () => SelectedTab != null)
    • Update CanExecute when SelectedTab changes

4. Add Menu Item to MainWindow

  • File: app/Sentinel.NLogViewer.App/MainWindow.xaml
    • Add new MenuItem "Logs" between "View" and "Help" menus
    • Structure: Logs -> Export -> *.log format
    • Bind to ExportLogsCommand from MainViewModel
    • Set IsEnabled binding to check if SelectedTab != null

5. Add Localization Resources (Optional)

  • File: app/Sentinel.NLogViewer.App/Resources/Resources.resx (and other language files)
    • Add keys: Menu_Logs, Menu_Export, Menu_ExportLogFormat

Technical Details

Export Format

The exported *.log file will use NLog standard format:

[dd-MM-yyyy hh:mm:ss.fff] [LEVEL] LoggerName: Message
Exception details (if present)

Finding NLogViewer Instance

Since NLogViewer is in a DataTemplate, we'll use visual tree traversal to find it:

  • Start from the TabControl's selected tab content
  • Traverse visual tree to find NLogViewer instance
  • Use VisualTreeHelper or recursive search

Filtered Log Entries

The export will use LogEvents.View.Cast<LogEventInfo>() which already applies all active filters (level filters and search terms).

Files to Modify

  1. app/Sentinel.NLogViewer.App/Models/ExportParameter.cs (new)
  2. ui/Sentinel.NLogViewer.Wpf/NLogViewer.xaml.cs
  3. app/Sentinel.NLogViewer.App/ViewModels/MainViewModel.cs
  4. app/Sentinel.NLogViewer.App/MainWindow.xaml
  5. app/Sentinel.NLogViewer.App/Resources/Resources.resx (optional)

Dependencies

  • Microsoft.Win32.SaveFileDialog (already used in MainViewModel)
  • System.Windows.Media.VisualTreeHelper for finding NLogViewer instance

Testing Checklist

  • Test export functionality with filtered logs
  • Test export with various log levels
  • Test export with search filters applied
  • Test menu item enable/disable based on tab selection
  • Test SaveFileDialog functionality
  • Verify exported file format matches NLog standard

Documentation

  • Document export functionality in README
  • Add screenshots of export menu and dialog

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions