- 创建 .NET 8 WPF 项目 PersonalToolBox - 引入 NuGet 包: CommunityToolkit.Mvvm, System.Text.Json, Microsoft.Extensions.DependencyInjection - 建立 MVVM 目录结构: Models/ ViewModels/ Views/ Services/ Helpers/ - 实现 LogEntry 模型 (时间/日志级别/内容) - 实现 ILogService 接口与 LogService (线程安全, 通过 Dispatcher 推送到 UI) - 配置 DI 容器 (App.xaml.cs) 注册日志服务和主窗口 - 添加 .gitignore 文件
51 lines
1.1 KiB
C#
51 lines
1.1 KiB
C#
namespace PersonalToolBox.Models;
|
|
|
|
/// <summary>
|
|
/// 日志级别
|
|
/// </summary>
|
|
public enum LogLevel
|
|
{
|
|
Info,
|
|
Warning,
|
|
Error
|
|
}
|
|
|
|
/// <summary>
|
|
/// 日志条目模型,用于底部信息栏的数据绑定
|
|
/// </summary>
|
|
public class LogEntry
|
|
{
|
|
/// <summary>
|
|
/// 日志产生时间
|
|
/// </summary>
|
|
public DateTime Timestamp { get; set; } = DateTime.Now;
|
|
|
|
/// <summary>
|
|
/// 日志级别 (Info / Warning / Error)
|
|
/// </summary>
|
|
public LogLevel Level { get; set; }
|
|
|
|
/// <summary>
|
|
/// 日志文本内容
|
|
/// </summary>
|
|
public string Content { get; set; } = string.Empty;
|
|
|
|
/// <summary>
|
|
/// 格式化后的时间字符串 (yyyy-MM-dd HH:mm:ss)
|
|
/// </summary>
|
|
public string FormattedTime => Timestamp.ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
/// <summary>
|
|
/// 日志级别的中文标记
|
|
/// </summary>
|
|
public string LevelText => Level switch
|
|
{
|
|
LogLevel.Info => "[信息]",
|
|
LogLevel.Warning => "[警告]",
|
|
LogLevel.Error => "[错误]",
|
|
_ => "[未知]"
|
|
};
|
|
|
|
public override string ToString() => $"[{FormattedTime}] {LevelText} {Content}";
|
|
}
|