完成显示日志功能
This commit is contained in:
@@ -1,50 +1,24 @@
|
||||
// 文件: DMS.WPF/ViewModels/Items/NlogItemViewModel.cs
|
||||
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using DMS.Core.Models;
|
||||
|
||||
namespace DMS.WPF.ViewModels.Items;
|
||||
|
||||
/// <summary>
|
||||
/// 代表日志列表中的单个日志项的ViewModel。
|
||||
/// 实现了INotifyPropertyChanged,其任何属性变化都会自动通知UI。
|
||||
/// </summary>
|
||||
public partial class NlogItemViewModel : ObservableObject
|
||||
public class NlogItemViewModel : ObservableObject
|
||||
{
|
||||
public int Id { get; set; }
|
||||
private Nlog _nlog;
|
||||
|
||||
[ObservableProperty]
|
||||
private DateTime _logTime;
|
||||
public NlogItemViewModel(Nlog nlog)
|
||||
{
|
||||
_nlog = nlog;
|
||||
}
|
||||
|
||||
[ObservableProperty]
|
||||
private string _level;
|
||||
|
||||
[ObservableProperty]
|
||||
private int _threadId;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _threadName;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _callsite;
|
||||
|
||||
[ObservableProperty]
|
||||
private int _callsiteLineNumber;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _message;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _logger;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _exception;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _callerFilePath;
|
||||
|
||||
[ObservableProperty]
|
||||
private int _callerLineNumber;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _callerMember;
|
||||
public int Id => _nlog.Id;
|
||||
public string Level => _nlog.Level;
|
||||
public string ThreadName => _nlog.ThreadName;
|
||||
public string Callsite => _nlog.Callsite;
|
||||
public string Message => _nlog.Message;
|
||||
public string Logger => _nlog.Logger;
|
||||
public string Exception => _nlog.Exception;
|
||||
public string StackTrace => _nlog.Exception; // Using Exception as StackTrace since it's not in the Nlog model
|
||||
public System.DateTime TimeStamp => _nlog.LogTime;
|
||||
}
|
||||
139
DMS.WPF/ViewModels/LogHistoryViewModel.cs
Normal file
139
DMS.WPF/ViewModels/LogHistoryViewModel.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
using AutoMapper;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Core.Models;
|
||||
using DMS.WPF.Interfaces;
|
||||
using DMS.WPF.ViewModels.Items;
|
||||
using ObservableCollections;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using DMS.WPF.ViewModels.Dialogs;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace DMS.WPF.ViewModels;
|
||||
|
||||
partial class LogHistoryViewModel : ViewModelBase
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly INlogAppService _nlogAppService;
|
||||
private readonly IDialogService _dialogService;
|
||||
private readonly INotificationService _notificationService;
|
||||
|
||||
[ObservableProperty]
|
||||
private NlogItemViewModel _selectedLog;
|
||||
|
||||
[ObservableProperty]
|
||||
private IList _selectedLogs = new ArrayList();
|
||||
|
||||
[ObservableProperty]
|
||||
private string _searchText;
|
||||
|
||||
private readonly ObservableList<NlogItemViewModel> _logItemList;
|
||||
private readonly ISynchronizedView<NlogItemViewModel, NlogItemViewModel> _synchronizedView;
|
||||
public NotifyCollectionChangedSynchronizedViewList<NlogItemViewModel> LogItemListView { get; }
|
||||
|
||||
public LogHistoryViewModel(IMapper mapper, INlogAppService nlogAppService, IDialogService dialogService, INotificationService notificationService)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_nlogAppService = nlogAppService;
|
||||
_dialogService = dialogService;
|
||||
_notificationService = notificationService;
|
||||
|
||||
_logItemList = new ObservableList<NlogItemViewModel>();
|
||||
_synchronizedView = _logItemList.CreateView(v => v);
|
||||
LogItemListView = _synchronizedView.ToNotifyCollectionChanged();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
partial void OnSearchTextChanged(string value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(SearchText))
|
||||
{
|
||||
_synchronizedView.ResetFilter();
|
||||
}
|
||||
else
|
||||
{
|
||||
_synchronizedView.AttachFilter(FilterLogs);
|
||||
}
|
||||
}
|
||||
|
||||
public override async void OnLoaded()
|
||||
{
|
||||
await LoadLogsAsync();
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private async Task RefreshLogsAsync()
|
||||
{
|
||||
await LoadLogsAsync();
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private async Task ClearLogsAsync()
|
||||
{
|
||||
var confirmDialogViewModel = new ConfirmDialogViewModel("确认", "确定要清空所有日志吗?", "确定");
|
||||
|
||||
var result = await _dialogService.ShowDialogAsync(confirmDialogViewModel);
|
||||
if (result == true)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _nlogAppService.ClearAllLogsAsync();
|
||||
_logItemList.Clear();
|
||||
_notificationService.ShowInfo("日志已清空");
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
_notificationService.ShowError($"清空日志时发生错误: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task LoadLogsAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var logs = await _nlogAppService.GetAllLogsAsync();
|
||||
var logItems = logs.Select(logDto =>
|
||||
{
|
||||
// Manually map NlogDto to Nlog
|
||||
var nlog = new Nlog
|
||||
{
|
||||
Id = logDto.Id,
|
||||
LogTime = logDto.LogTime,
|
||||
Level = logDto.Level,
|
||||
ThreadId = logDto.ThreadId,
|
||||
ThreadName = logDto.ThreadName,
|
||||
Callsite = logDto.Callsite,
|
||||
CallsiteLineNumber = logDto.CallsiteLineNumber,
|
||||
Message = logDto.Message,
|
||||
Logger = logDto.Logger,
|
||||
Exception = logDto.Exception,
|
||||
CallerFilePath = logDto.CallerFilePath,
|
||||
CallerLineNumber = logDto.CallerLineNumber,
|
||||
CallerMember = logDto.CallerMember
|
||||
};
|
||||
return new NlogItemViewModel(nlog);
|
||||
}).ToList();
|
||||
|
||||
_logItemList.Clear();
|
||||
_logItemList.AddRange(logItems);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
_notificationService.ShowError($"加载日志时发生错误: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user