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
app/Sentinel.NLogViewer.App/Models/ExportParameter.cs (new)
ui/Sentinel.NLogViewer.Wpf/NLogViewer.xaml.cs
app/Sentinel.NLogViewer.App/ViewModels/MainViewModel.cs
app/Sentinel.NLogViewer.App/MainWindow.xaml
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
Documentation
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
app/Sentinel.NLogViewer.App/Models/ExportParameter.csExportParameterclass with:FilePath(string) - target file pathFormat(ExportFormat enum) - export format typeExportFormatenum withLogas default value (extensible for future formats)2. Add Export Command to NLogViewer
ui/Sentinel.NLogViewer.Wpf/NLogViewer.xaml.csExportCommanddependency property (ICommand)GetFilteredLogEntries()that returnsIEnumerable<LogEventInfo>fromLogEvents.View(filtered collection)ExportLogs(ExportParameter parameter)that:GetFilteredLogEntries()[Timestamp] [Level] LoggerName: Message(with exception if present)ExportCommandin constructor asRelayCommand<ExportParameter>(ExportLogs)3. Add Export Command to MainViewModel
app/Sentinel.NLogViewer.App/ViewModels/MainViewModel.csExportLogsCommandproperty (ICommand)ExportLogs()method that:SelectedTab != nullSaveFileDialogwith filter"Log Files (*.log)|*.log"NLogViewerinstance from selected tab's visual tree (using helper method)ExportParameterwith file path andExportFormat.LogNLogViewer.ExportCommand.Execute(exportParameter)FindNLogViewerInTab()to locate NLogViewer instanceExportLogsCommand = new RelayCommand(ExportLogs, () => SelectedTab != null)CanExecutewhenSelectedTabchanges4. Add Menu Item to MainWindow
app/Sentinel.NLogViewer.App/MainWindow.xamlMenuItem"Logs" between "View" and "Help" menusLogs -> Export -> *.log formatExportLogsCommandfrom MainViewModelIsEnabledbinding to check ifSelectedTab != null5. Add Localization Resources (Optional)
app/Sentinel.NLogViewer.App/Resources/Resources.resx(and other language files)Menu_Logs,Menu_Export,Menu_ExportLogFormatTechnical Details
Export Format
The exported *.log file will use NLog standard format:
Finding NLogViewer Instance
Since NLogViewer is in a DataTemplate, we'll use visual tree traversal to find it:
VisualTreeHelperor recursive searchFiltered Log Entries
The export will use
LogEvents.View.Cast<LogEventInfo>()which already applies all active filters (level filters and search terms).Files to Modify
app/Sentinel.NLogViewer.App/Models/ExportParameter.cs(new)ui/Sentinel.NLogViewer.Wpf/NLogViewer.xaml.csapp/Sentinel.NLogViewer.App/ViewModels/MainViewModel.csapp/Sentinel.NLogViewer.App/MainWindow.xamlapp/Sentinel.NLogViewer.App/Resources/Resources.resx(optional)Dependencies
Microsoft.Win32.SaveFileDialog(already used in MainViewModel)System.Windows.Media.VisualTreeHelperfor finding NLogViewer instanceTesting Checklist
Documentation