diff --git a/DMS.WPF/ViewModels/LogHistoryViewModel.cs b/DMS.WPF/ViewModels/LogHistoryViewModel.cs index e91d6e1..4fb9aeb 100644 --- a/DMS.WPF/ViewModels/LogHistoryViewModel.cs +++ b/DMS.WPF/ViewModels/LogHistoryViewModel.cs @@ -13,6 +13,8 @@ using System.Linq; using System.Threading.Tasks; using DMS.WPF.ViewModels.Dialogs; using Microsoft.Extensions.DependencyInjection; +using System.Collections.ObjectModel; +using System; namespace DMS.WPF.ViewModels; @@ -32,10 +34,15 @@ partial class LogHistoryViewModel : ViewModelBase [ObservableProperty] private string _searchText; + [ObservableProperty] + private string _selectedLogLevel; + private readonly ObservableList _logItemList; private readonly ISynchronizedView _synchronizedView; public NotifyCollectionChangedSynchronizedViewList LogItemListView { get; } + public ObservableCollection LogLevels { get; } = new ObservableCollection { "Trace", "Debug", "Info", "Warn", "Error", "Fatal" }; + public LogHistoryViewModel(IMapper mapper, INlogAppService nlogAppService, IDialogService dialogService, INotificationService notificationService) { _mapper = mapper; @@ -50,16 +57,34 @@ partial class LogHistoryViewModel : ViewModelBase private bool FilterLogs(NlogItemViewModel item) { - var searchTextLower = SearchText.ToLower(); - return item.Logger?.ToLower().Contains(searchTextLower) == true || - item.Message?.ToLower().Contains(searchTextLower) == true || - item.Exception?.ToLower().Contains(searchTextLower) == true || - item.StackTrace?.ToLower().Contains(searchTextLower) == true; + // 搜索文本过滤 + var searchTextLower = SearchText?.ToLower() ?? string.Empty; + var searchTextMatch = string.IsNullOrWhiteSpace(SearchText) || + item.Logger?.ToLower().Contains(searchTextLower) == true || + item.Message?.ToLower().Contains(searchTextLower) == true || + item.Exception?.ToLower().Contains(searchTextLower) == true || + item.StackTrace?.ToLower().Contains(searchTextLower) == true; + + // 日志级别过滤 + var levelMatch = string.IsNullOrWhiteSpace(SelectedLogLevel) || + item.Level?.Equals(SelectedLogLevel, StringComparison.OrdinalIgnoreCase) == true; + + return searchTextMatch && levelMatch; } partial void OnSearchTextChanged(string value) { - if (string.IsNullOrWhiteSpace(SearchText)) + ApplyFilter(); + } + + partial void OnSelectedLogLevelChanged(string value) + { + ApplyFilter(); + } + + private void ApplyFilter() + { + if (string.IsNullOrWhiteSpace(SearchText) && string.IsNullOrWhiteSpace(SelectedLogLevel)) { _synchronizedView.ResetFilter(); } @@ -106,7 +131,11 @@ partial class LogHistoryViewModel : ViewModelBase try { var logs = await _nlogAppService.GetAllLogsAsync(); - var logItems = logs.Select(logDto => + + // 按时间倒序排序 + var sortedLogs = logs.OrderByDescending(logDto => logDto.LogTime).ToList(); + + var logItems = sortedLogs.Select(logDto => { // Manually map NlogDto to Nlog var nlog = new Nlog diff --git a/DMS.WPF/Views/LogHistoryView.xaml b/DMS.WPF/Views/LogHistoryView.xaml index 45cc055..8395af1 100644 --- a/DMS.WPF/Views/LogHistoryView.xaml +++ b/DMS.WPF/Views/LogHistoryView.xaml @@ -80,6 +80,15 @@ HorizontalAlignment="Left" ui:ControlHelper.PlaceholderText="搜索日志..." Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}" /> + + + @@ -90,7 +99,7 @@ Margin="10" AutoGenerateColumns="False" CanUserDeleteRows="False" - CanUserSortColumns="False" + CanUserSortColumns="True" IsReadOnly="True" ItemsSource="{Binding LogItemListView}" SelectedItem="{Binding SelectedLog}"