From 3489f8a064a92fa7aba10872fb90ba05bc24f163 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Sun, 7 Sep 2025 20:04:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=BB=98=E8=AE=A4=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=E5=92=8C=E6=8C=89=E7=85=A7=E7=AD=89=E7=BA=A7=E7=AD=9B?= =?UTF-8?q?=E9=80=89=E6=97=A5=E5=BF=97=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DMS.WPF/ViewModels/LogHistoryViewModel.cs | 43 +++++++++++++++++++---- DMS.WPF/Views/LogHistoryView.xaml | 11 +++++- 2 files changed, 46 insertions(+), 8 deletions(-) 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}"