初步完成Log的增删改查
This commit is contained in:
72
DMS.Application/DTOs/NlogDto.cs
Normal file
72
DMS.Application/DTOs/NlogDto.cs
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
namespace DMS.Application.DTOs;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 用于在UI上显示NLog日志条目的DTO。
|
||||||
|
/// </summary>
|
||||||
|
public class NlogDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 日志ID。
|
||||||
|
/// </summary>
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 日志记录时间。
|
||||||
|
/// </summary>
|
||||||
|
public DateTime LogTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 日志级别 (如 INFO, ERROR 等)。
|
||||||
|
/// </summary>
|
||||||
|
public string Level { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 线程ID。
|
||||||
|
/// </summary>
|
||||||
|
public int ThreadId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 线程名称。
|
||||||
|
/// </summary>
|
||||||
|
public string ThreadName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调用点(通常是记录日志的方法名)。
|
||||||
|
/// </summary>
|
||||||
|
public string Callsite { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调用点所在的行号。
|
||||||
|
/// </summary>
|
||||||
|
public int CallsiteLineNumber { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 日志消息内容。
|
||||||
|
/// </summary>
|
||||||
|
public string Message { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 记录日志的记录器名称。
|
||||||
|
/// </summary>
|
||||||
|
public string Logger { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异常信息(如果有的话)。
|
||||||
|
/// </summary>
|
||||||
|
public string Exception { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调用方文件路径。
|
||||||
|
/// </summary>
|
||||||
|
public string CallerFilePath { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调用方行号。
|
||||||
|
/// </summary>
|
||||||
|
public int CallerLineNumber { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调用方成员(方法名)。
|
||||||
|
/// </summary>
|
||||||
|
public string CallerMember { get; set; }
|
||||||
|
}
|
||||||
30
DMS.Application/Interfaces/INlogAppService.cs
Normal file
30
DMS.Application/Interfaces/INlogAppService.cs
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
using DMS.Application.DTOs;
|
||||||
|
|
||||||
|
namespace DMS.Application.Interfaces;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 定义Nlog日志管理相关的应用服务操作。
|
||||||
|
/// </summary>
|
||||||
|
public interface INlogAppService
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 异步根据ID获取Nlog日志DTO。
|
||||||
|
/// </summary>
|
||||||
|
Task<NlogDto> GetLogByIdAsync(int id);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步获取所有Nlog日志DTO列表。
|
||||||
|
/// </summary>
|
||||||
|
Task<List<NlogDto>> GetAllLogsAsync();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步获取指定数量的最新Nlog日志DTO列表。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="count">要获取的日志条目数量。</param>
|
||||||
|
Task<List<NlogDto>> GetLatestLogsAsync(int count);
|
||||||
|
|
||||||
|
// 可以在这里添加更多针对日志的查询服务方法,例如:
|
||||||
|
// Task<List<NlogDto>> GetLogsByLevelAsync(string level);
|
||||||
|
// Task<List<NlogDto>> GetLogsByDateRangeAsync(DateTime startDate, DateTime endDate);
|
||||||
|
// Task<List<NlogDto>> SearchLogsAsync(string searchTerm);
|
||||||
|
}
|
||||||
90
DMS.Application/Services/NlogAppService.cs
Normal file
90
DMS.Application/Services/NlogAppService.cs
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using DMS.Application.DTOs;
|
||||||
|
using DMS.Application.Interfaces;
|
||||||
|
using DMS.Core.Interfaces;
|
||||||
|
|
||||||
|
namespace DMS.Application.Services;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Nlog日志应用服务,负责处理Nlog日志相关的业务逻辑。
|
||||||
|
/// 实现 <see cref="INlogAppService"/> 接口。
|
||||||
|
/// </summary>
|
||||||
|
public class NlogAppService : INlogAppService
|
||||||
|
{
|
||||||
|
private readonly IRepositoryManager _repoManager;
|
||||||
|
private readonly IMapper _mapper;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构造函数,通过依赖注入获取仓储管理器和AutoMapper实例。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="repoManager">仓储管理器实例。</param>
|
||||||
|
/// <param name="mapper">AutoMapper 实例。</param>
|
||||||
|
public NlogAppService(IRepositoryManager repoManager, IMapper mapper)
|
||||||
|
{
|
||||||
|
_repoManager = repoManager;
|
||||||
|
_mapper = mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步根据ID获取Nlog日志数据传输对象。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">日志ID。</param>
|
||||||
|
/// <returns>Nlog日志数据传输对象。</returns>
|
||||||
|
public async Task<NlogDto> GetLogByIdAsync(int id)
|
||||||
|
{
|
||||||
|
var log = await _repoManager.Nlogs.GetByIdAsync(id);
|
||||||
|
return _mapper.Map<NlogDto>(log);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步获取所有Nlog日志数据传输对象列表。
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Nlog日志数据传输对象列表。</returns>
|
||||||
|
public async Task<List<NlogDto>> GetAllLogsAsync()
|
||||||
|
{
|
||||||
|
var logs = await _repoManager.Nlogs.GetAllAsync();
|
||||||
|
return _mapper.Map<List<NlogDto>>(logs);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步获取指定数量的最新Nlog日志数据传输对象列表。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="count">要获取的日志条目数量。</param>
|
||||||
|
/// <returns>最新的Nlog日志数据传输对象列表。</returns>
|
||||||
|
public async Task<List<NlogDto>> GetLatestLogsAsync(int count)
|
||||||
|
{
|
||||||
|
// 注意:这里的实现假设仓储层或数据库支持按时间倒序排列并取前N条。
|
||||||
|
// 如果 BaseRepository 没有提供这种能力,可能需要直接访问 DbNlog 实体。
|
||||||
|
// 例如:var dbLogs = await _repoManager.Nlogs.Db.Queryable<Infrastructure.Entities.DbNlog>().OrderByDescending(n => n.LogTime).Take(count).ToListAsync();
|
||||||
|
// var logs = _mapper.Map<List<Core.Models.Nlog>>(dbLogs);
|
||||||
|
// return _mapper.Map<List<NlogDto>>(logs);
|
||||||
|
|
||||||
|
// 为简化起见,这里先调用 GetAll 然后在内存中排序和截取(仅适用于日志量不大的情况)。
|
||||||
|
// 生产环境中建议优化数据库查询。
|
||||||
|
var allLogs = await GetAllLogsAsync();
|
||||||
|
return allLogs.OrderByDescending(l => l.LogTime).Take(count).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 可以在这里实现 INlogAppService 接口中定义的其他方法
|
||||||
|
// 例如:
|
||||||
|
/*
|
||||||
|
public async Task<List<NlogDto>> GetLogsByLevelAsync(string level)
|
||||||
|
{
|
||||||
|
// 假设 INlogRepository 有 GetLogsByLevelAsync 方法
|
||||||
|
var logs = await _repoManager.Nlogs.GetLogsByLevelAsync(level);
|
||||||
|
return _mapper.Map<List<NlogDto>>(logs);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<NlogDto>> GetLogsByDateRangeAsync(DateTime startDate, DateTime endDate)
|
||||||
|
{
|
||||||
|
var logs = await _repoManager.Nlogs.GetLogsByDateRangeAsync(startDate, endDate);
|
||||||
|
return _mapper.Map<List<NlogDto>>(logs);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<NlogDto>> SearchLogsAsync(string searchTerm)
|
||||||
|
{
|
||||||
|
var logs = await _repoManager.Nlogs.SearchLogsAsync(searchTerm);
|
||||||
|
return _mapper.Map<List<NlogDto>>(logs);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
@@ -49,6 +49,11 @@ public interface IRepositoryManager : IDisposable
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
IUserRepository Users { get; set; }
|
IUserRepository Users { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取Nlog日志仓储的实例。
|
||||||
|
/// </summary>
|
||||||
|
INlogRepository Nlogs { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 初始化数据库
|
/// 初始化数据库
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
15
DMS.Core/Interfaces/Repositories/INlogRepository.cs
Normal file
15
DMS.Core/Interfaces/Repositories/INlogRepository.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using DMS.Core.Models;
|
||||||
|
|
||||||
|
namespace DMS.Core.Interfaces.Repositories;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Nlog日志仓储接口,定义了对Nlog实体的特定数据访问方法。
|
||||||
|
/// 继承自通用仓储接口 IBaseRepository<Nlog>。
|
||||||
|
/// </summary>
|
||||||
|
public interface INlogRepository : IBaseRepository<Nlog>
|
||||||
|
{
|
||||||
|
// 可以在此处添加 Nlog 特定的查询方法,例如:
|
||||||
|
// Task<List<Nlog>> GetLogsByLevelAsync(string level);
|
||||||
|
// Task<List<Nlog>> GetLogsByDateRangeAsync(DateTime startDate, DateTime endDate);
|
||||||
|
// Task<List<Nlog>> SearchLogsAsync(string searchTerm);
|
||||||
|
}
|
||||||
74
DMS.Core/Models/Nlog.cs
Normal file
74
DMS.Core/Models/Nlog.cs
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace DMS.Core.Models;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 代表一条NLog日志记录。
|
||||||
|
/// </summary>
|
||||||
|
public class Nlog
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 日志ID。
|
||||||
|
/// </summary>
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 日志记录时间。
|
||||||
|
/// </summary>
|
||||||
|
public DateTime LogTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 日志级别 (如 INFO, ERROR 等)。
|
||||||
|
/// </summary>
|
||||||
|
public string Level { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 线程ID。
|
||||||
|
/// </summary>
|
||||||
|
public int ThreadId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 线程名称。
|
||||||
|
/// </summary>
|
||||||
|
public string ThreadName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调用点(通常是记录日志的方法名)。
|
||||||
|
/// </summary>
|
||||||
|
public string Callsite { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调用点所在的行号。
|
||||||
|
/// </summary>
|
||||||
|
public int CallsiteLineNumber { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 日志消息内容。
|
||||||
|
/// </summary>
|
||||||
|
public string Message { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 记录日志的记录器名称。
|
||||||
|
/// </summary>
|
||||||
|
public string Logger { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异常信息(如果有的话)。
|
||||||
|
/// </summary>
|
||||||
|
public string Exception { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调用方文件路径。
|
||||||
|
/// </summary>
|
||||||
|
public string CallerFilePath { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调用方行号。
|
||||||
|
/// </summary>
|
||||||
|
public int CallerLineNumber { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调用方成员(方法名)。
|
||||||
|
/// </summary>
|
||||||
|
public string CallerMember { get; set; }
|
||||||
|
}
|
||||||
88
DMS.Infrastructure/Entities/DbNlog.cs
Normal file
88
DMS.Infrastructure/Entities/DbNlog.cs
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
using SqlSugar;
|
||||||
|
|
||||||
|
namespace DMS.Infrastructure.Entities;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// NLog日志实体类,对应数据库中的 nlog 表。
|
||||||
|
/// </summary>
|
||||||
|
[SugarTable("nlog")]
|
||||||
|
public class DbNlog
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 日志ID,主键且自增。
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(IsPrimaryKey = true, IsIdentity = true, ColumnName = "ID")]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 日志记录时间。
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnName = "LogTime")]
|
||||||
|
public DateTime LogTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 日志级别 (如 INFO, ERROR 等)。
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnName = "Level")]
|
||||||
|
public string Level { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 线程ID。
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnName = "ThreadID")]
|
||||||
|
public int ThreadId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 线程名称。
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnName = "ThreadName", IsNullable = true)]
|
||||||
|
public string ThreadName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调用点(通常是记录日志的方法名)。
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnName = "Callsite", IsNullable = true)]
|
||||||
|
public string Callsite { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调用点所在的行号。
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnName = "CallsiteLineNumber")]
|
||||||
|
public int CallsiteLineNumber { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 日志消息内容。
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnName = "Message")]
|
||||||
|
public string Message { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 记录日志的记录器名称。
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnName = "Logger")]
|
||||||
|
public string Logger { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异常信息(如果有的话)。
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnName = "Exception", IsNullable = true)]
|
||||||
|
public string Exception { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调用方文件路径。
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnName = "CallerFilePath", IsNullable = true)]
|
||||||
|
public string CallerFilePath { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调用方行号。
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnName = "CallerLineNumber")]
|
||||||
|
public int CallerLineNumber { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调用方成员(方法名)。
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnName = "CallerMember", IsNullable = true)]
|
||||||
|
public string CallerMember { get; set; }
|
||||||
|
}
|
||||||
130
DMS.Infrastructure/Repositories/NlogRepository.cs
Normal file
130
DMS.Infrastructure/Repositories/NlogRepository.cs
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using DMS.Core.Interfaces.Repositories;
|
||||||
|
using DMS.Core.Models;
|
||||||
|
using DMS.Infrastructure.Data;
|
||||||
|
using DMS.Infrastructure.Entities;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace DMS.Infrastructure.Repositories;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Nlog日志仓储实现类,负责Nlog日志数据的持久化操作。
|
||||||
|
/// 继承自 <see cref="BaseRepository{DbNlog}"/> 并实现 <see cref="INlogRepository"/> 接口。
|
||||||
|
/// </summary>
|
||||||
|
public class NlogRepository : BaseRepository<DbNlog>, INlogRepository
|
||||||
|
{
|
||||||
|
private readonly IMapper _mapper;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构造函数,注入 AutoMapper 和 SqlSugarDbContext。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="mapper">AutoMapper 实例,用于实体模型和数据库模型之间的映射。</param>
|
||||||
|
/// <param name="dbContext">SqlSugar 数据库上下文,用于数据库操作。</param>
|
||||||
|
/// <param name="logger">日志记录器实例。</param>
|
||||||
|
public NlogRepository(IMapper mapper, SqlSugarDbContext dbContext, ILogger<NlogRepository> logger)
|
||||||
|
: base(dbContext, logger)
|
||||||
|
{
|
||||||
|
_mapper = mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nlog 通常是只读或追加的日志,因此像 AddAsync, UpdateAsync, DeleteAsync 这样的修改方法
|
||||||
|
// 可能不需要在仓储接口中暴露,或者可以省略具体实现或抛出 NotSupportedException。
|
||||||
|
// 但为了保持与基类一致性并满足接口要求,这里显式实现它们。
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步获取所有Nlog日志条目。
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>包含所有Nlog日志实体的列表。</returns>
|
||||||
|
public new async Task<List<Core.Models.Nlog>> GetAllAsync()
|
||||||
|
{
|
||||||
|
var dbList = await base.GetAllAsync();
|
||||||
|
return _mapper.Map<List<Core.Models.Nlog>>(dbList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步根据ID获取单个Nlog日志条目。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">日志条目的唯一标识符。</param>
|
||||||
|
/// <returns>对应的Nlog日志实体,如果不存在则为null。</returns>
|
||||||
|
public new async Task<Core.Models.Nlog> GetByIdAsync(int id)
|
||||||
|
{
|
||||||
|
var dbNlog = await base.GetByIdAsync(id);
|
||||||
|
return _mapper.Map<Core.Models.Nlog>(dbNlog);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步添加一个新Nlog日志条目。
|
||||||
|
/// 注意:直接写入数据库日志表通常不推荐,NLog本身负责写入。
|
||||||
|
/// 此方法主要用于满足接口契约,实际使用应谨慎。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="entity">要添加的Nlog日志实体。</param>
|
||||||
|
/// <returns>添加后的Nlog日志实体(包含ID等信息)。</returns>
|
||||||
|
public new async Task<Core.Models.Nlog> AddAsync(Core.Models.Nlog entity)
|
||||||
|
{
|
||||||
|
var dbEntity = _mapper.Map<DbNlog>(entity);
|
||||||
|
var addedDbEntity = await base.AddAsync(dbEntity);
|
||||||
|
return _mapper.Map(addedDbEntity, entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步更新一个Nlog日志条目。
|
||||||
|
/// 注意:直接更新数据库日志表通常不推荐,日志应视为不可变。
|
||||||
|
/// 此方法主要用于满足接口契约,实际使用应谨慎。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="entity">要更新的Nlog日志实体。</param>
|
||||||
|
/// <returns>受影响的行数。</returns>
|
||||||
|
public new async Task<int> UpdateAsync(Core.Models.Nlog entity)
|
||||||
|
{
|
||||||
|
var dbEntity = _mapper.Map<DbNlog>(entity);
|
||||||
|
return await base.UpdateAsync(dbEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步删除一个Nlog日志条目。
|
||||||
|
/// 注意:直接删除数据库日志表记录通常不推荐,除非是清理过期日志。
|
||||||
|
/// 此方法主要用于满足接口契约,实际使用应谨慎。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="entity">要删除的Nlog日志实体。</param>
|
||||||
|
/// <returns>受影响的行数。</returns>
|
||||||
|
public new async Task<int> DeleteAsync(Core.Models.Nlog entity)
|
||||||
|
{
|
||||||
|
var dbEntity = _mapper.Map<DbNlog>(entity);
|
||||||
|
return await base.DeleteAsync(dbEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步根据ID删除一个Nlog日志条目。
|
||||||
|
/// 注意:直接删除数据库日志表记录通常不推荐,除非是清理过期日志。
|
||||||
|
/// 此方法主要用于满足接口契约,实际使用应谨慎。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">要删除的Nlog日志条目的ID。</param>
|
||||||
|
/// <returns>受影响的行数。</returns>
|
||||||
|
public new async Task<int> DeleteByIdAsync(int id)
|
||||||
|
{
|
||||||
|
return await base.DeleteByIdsAsync(new List<int>(){id});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步获取指定数量的Nlog日志条目。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="number">要获取的条目数量。</param>
|
||||||
|
/// <returns>包含指定数量Nlog日志实体的列表。</returns>
|
||||||
|
public new async Task<List<Core.Models.Nlog>> TakeAsync(int number)
|
||||||
|
{
|
||||||
|
var dbList = await base.TakeAsync(number);
|
||||||
|
return _mapper.Map<List<Core.Models.Nlog>>(dbList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步批量添加Nlog日志条目。
|
||||||
|
/// 注意:直接批量写入数据库日志表通常不推荐,NLog本身负责写入。
|
||||||
|
/// 此方法主要用于满足接口契约,实际使用应谨慎。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="entities">要添加的Nlog日志实体列表。</param>
|
||||||
|
/// <returns>操作是否成功。</returns>
|
||||||
|
public new async Task<bool> AddBatchAsync(List<Core.Models.Nlog> entities)
|
||||||
|
{
|
||||||
|
var dbEntities = _mapper.Map<List<DbNlog>>(entities);
|
||||||
|
return await base.AddBatchAsync(dbEntities);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -27,6 +27,7 @@ public class RepositoryManager : IRepositoryManager
|
|||||||
/// <param name="menus">菜单仓储实例。</param>
|
/// <param name="menus">菜单仓储实例。</param>
|
||||||
/// <param name="variableHistories">变量历史仓储实例。</param>
|
/// <param name="variableHistories">变量历史仓储实例。</param>
|
||||||
/// <param name="users">用户仓储实例。</param>
|
/// <param name="users">用户仓储实例。</param>
|
||||||
|
/// <param name="nlogs">Nlog日志仓储实例。</param>
|
||||||
public RepositoryManager( SqlSugarDbContext dbContext,
|
public RepositoryManager( SqlSugarDbContext dbContext,
|
||||||
IInitializeRepository initializeRepository,
|
IInitializeRepository initializeRepository,
|
||||||
IDeviceRepository devices,
|
IDeviceRepository devices,
|
||||||
@@ -36,7 +37,8 @@ public class RepositoryManager : IRepositoryManager
|
|||||||
IVariableMqttAliasRepository variableMqttAliases,
|
IVariableMqttAliasRepository variableMqttAliases,
|
||||||
IMenuRepository menus,
|
IMenuRepository menus,
|
||||||
IVariableHistoryRepository variableHistories,
|
IVariableHistoryRepository variableHistories,
|
||||||
IUserRepository users)
|
IUserRepository users,
|
||||||
|
INlogRepository nlogs)
|
||||||
{
|
{
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
InitializeRepository = initializeRepository;
|
InitializeRepository = initializeRepository;
|
||||||
@@ -48,6 +50,7 @@ public class RepositoryManager : IRepositoryManager
|
|||||||
Menus = menus;
|
Menus = menus;
|
||||||
VariableHistories = variableHistories;
|
VariableHistories = variableHistories;
|
||||||
Users = users;
|
Users = users;
|
||||||
|
Nlogs = nlogs;
|
||||||
|
|
||||||
_db = dbContext.GetInstance();
|
_db = dbContext.GetInstance();
|
||||||
}
|
}
|
||||||
@@ -93,6 +96,10 @@ public class RepositoryManager : IRepositoryManager
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public IUserRepository Users { get; set; }
|
public IUserRepository Users { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
/// 获取Nlog日志仓储实例。
|
||||||
|
/// </summary>
|
||||||
|
public INlogRepository Nlogs { get; set; }
|
||||||
|
/// <summary>
|
||||||
/// 获取初始化仓储实例。
|
/// 获取初始化仓储实例。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IInitializeRepository InitializeRepository { get; set; }
|
public IInitializeRepository InitializeRepository { get; set; }
|
||||||
|
|||||||
@@ -204,6 +204,7 @@ public partial class App : System.Windows.Application
|
|||||||
services.AddSingleton<IMenuRepository, MenuRepository>();
|
services.AddSingleton<IMenuRepository, MenuRepository>();
|
||||||
services.AddSingleton<IVariableHistoryRepository, VariableHistoryRepository>();
|
services.AddSingleton<IVariableHistoryRepository, VariableHistoryRepository>();
|
||||||
services.AddSingleton<IUserRepository, UserRepository>();
|
services.AddSingleton<IUserRepository, UserRepository>();
|
||||||
|
services.AddSingleton<INlogRepository, NlogRepository>();
|
||||||
services.AddSingleton<IRepositoryManager, RepositoryManager>();
|
services.AddSingleton<IRepositoryManager, RepositoryManager>();
|
||||||
services.AddSingleton<IExcelService, ExcelService>();
|
services.AddSingleton<IExcelService, ExcelService>();
|
||||||
|
|
||||||
|
|||||||
50
DMS.WPF/ViewModels/Items/NlogItemViewModel.cs
Normal file
50
DMS.WPF/ViewModels/Items/NlogItemViewModel.cs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
// 文件: DMS.WPF/ViewModels/Items/NlogItemViewModel.cs
|
||||||
|
|
||||||
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
|
||||||
|
namespace DMS.WPF.ViewModels.Items;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 代表日志列表中的单个日志项的ViewModel。
|
||||||
|
/// 实现了INotifyPropertyChanged,其任何属性变化都会自动通知UI。
|
||||||
|
/// </summary>
|
||||||
|
public partial class NlogItemViewModel : ObservableObject
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
private DateTime _logTime;
|
||||||
|
|
||||||
|
[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;
|
||||||
|
}
|
||||||
66
QWEN.md
66
QWEN.md
@@ -1,48 +1,48 @@
|
|||||||
# Project Overview: DMS (Device Management System)
|
# 项目概述:DMS (Device Management System - 设备管理系统)
|
||||||
|
|
||||||
This directory contains the source code for the Device Management System (DMS), a C#/.NET 8 application. The system is primarily composed of a WPF desktop application for the user interface, backed by a layered architecture including Core, Application, and Infrastructure projects. It also includes dedicated unit test projects for both the infrastructure and WPF layers.
|
此目录包含了设备管理系统 (DMS) 的源代码,这是一个基于 C#/.NET 8 构建的应用程序。该系统主要由一个用于用户界面的 WPF 桌面应用程序组成,后端采用分层架构,包括 Core(核心)、Application(应用)和 Infrastructure(基础设施)项目。此外,还为基础设施层和 WPF 层配备了专门的单元测试项目。
|
||||||
|
|
||||||
A significant feature described in the README is an OPC UA service implementation, suggesting the system is designed for industrial device communication and management.
|
项目自述文件中提到的一个重要特性是 OPC UA 服务实现,表明该系统是为工业设备通信和管理而设计的。
|
||||||
|
|
||||||
## Project Structure
|
## 项目结构
|
||||||
|
|
||||||
The solution (`DMS.sln`) is organized into several key projects:
|
解决方案 (`DMS.sln`) 由几个关键项目组成:
|
||||||
|
|
||||||
* **`DMS.Core`**: Contains fundamental, reusable components and shared models. Likely includes utilities, common data transfer objects (DTOs), and core business logic abstractions. It references `Microsoft.Extensions.Logging.Abstractions`, `Newtonsoft.Json`, and `NLog`.
|
* **`DMS.Core`**: 包含基础的、可复用的组件和共享模型。可能包括实用工具、通用数据传输对象 (DTOs) 以及核心业务逻辑抽象。它引用了 `Microsoft.Extensions.Logging.Abstractions`、`Newtonsoft.Json` 和 `NLog`。
|
||||||
* **`DMS.Application`**: Implements application-specific business logic. It depends on `DMS.Core` and uses libraries like `AutoMapper` for object mapping and `Microsoft.Extensions` for hosting and logging abstractions.
|
* **`DMS.Application`**: 实现应用特定的业务逻辑。它依赖于 `DMS.Core`,并使用 `AutoMapper`(用于对象映射)和 `Microsoft.Extensions`(用于托管和日志记录抽象)等库。
|
||||||
* **`DMS.Infrastructure`**: Handles data access, external integrations, and technical implementations. It references `DMS.Core` and `DMS.Application`. Key dependencies include:
|
* **`DMS.Infrastructure`**: 处理数据访问、外部集成和技术实现。它引用了 `DMS.Core` 和 `DMS.Application`。关键依赖包括:
|
||||||
* `SqlSugarCore`: For database interaction (MySQL).
|
* `SqlSugarCore`:用于数据库交互(MySQL)。
|
||||||
* `MQTTnet`: For MQTT protocol communication.
|
* `MQTTnet`:用于 MQTT 协议通信。
|
||||||
* `OPCFoundation.NetStandard.Opc.Ua`: For OPC UA communication.
|
* `OPCFoundation.NetStandard.Opc.Ua`:用于 OPC UA 通信。
|
||||||
* `S7netplus`: For Siemens S7 PLC communication.
|
* `S7netplus`:用于西门子 S7 PLC 通信。
|
||||||
* `NPOI`: For reading/writing Excel files.
|
* `NPOI`:用于读写 Excel 文件。
|
||||||
* `AutoMapper.Extensions.Microsoft.DependencyInjection`: For dependency injection setup of AutoMapper.
|
* `AutoMapper.Extensions.Microsoft.DependencyInjection`:用于 AutoMapper 的依赖注入设置。
|
||||||
* **`DMS.WPF`**: The main WPF desktop application. It targets `net8.0-windows` and uses WPF-specific libraries like `CommunityToolkit.Mvvm`, `HandyControl` (UI toolkit), `Hardcodet.NotifyIcon.Wpf` (system tray icon), and `iNKORE.UI.WPF.Modern` (modern UI styling). It depends on `DMS.Application` and `DMS.Infrastructure`.
|
* **`DMS.WPF`**:主 WPF 桌面应用程序。它以 `net8.0-windows` 为目标,并使用 WPF 特定的库,如 `CommunityToolkit.Mvvm`、`HandyControl`(UI 工具包)、`Hardcodet.NotifyIcon.Wpf`(系统托盘图标)和 `iNKORE.UI.WPF.Modern`(现代 UI 样式)。它依赖于 `DMS.Application` 和 `DMS.Infrastructure`。
|
||||||
* **`DMS.Infrastructure.UnitTests`**: Unit tests for the `DMS.Infrastructure` project. Uses `xunit`, `Moq`, `Bogus` (for fake data), and `Microsoft.NET.Test.Sdk`.
|
* **`DMS.Infrastructure.UnitTests`**:针对 `DMS.Infrastructure` 项目的单元测试。使用 `xunit`、`Moq`、`Bogus`(用于伪造数据)和 `Microsoft.NET.Test.Sdk`。
|
||||||
* **`DMS.WPF.UnitTests`**: Unit tests for the `DMS.WPF` project. Also uses `xunit`, `Moq`, and `Bogus`.
|
* **`DMS.WPF.UnitTests`**:针对 `DMS.WPF` 项目的单元测试。同样使用 `xunit`、`Moq` 和 `Bogus`。
|
||||||
|
|
||||||
## Building and Running
|
## 构建与运行
|
||||||
|
|
||||||
This is a .NET 8 solution. You will need the .NET 8 SDK and an IDE like Visual Studio or JetBrains Rider to build and run it.
|
这是一个 .NET 8 解决方案。您需要安装 .NET 8 SDK 以及 Visual Studio 或 JetBrains Rider 等 IDE 来构建和运行它。
|
||||||
|
|
||||||
**To Build:**
|
**构建:**
|
||||||
Use the standard .NET CLI command within the solution directory:
|
在解决方案目录下使用标准 .NET CLI 命令:
|
||||||
`dotnet build`
|
`dotnet build`
|
||||||
|
|
||||||
**To Run:**
|
**运行:**
|
||||||
The main executable is the WPF application (`DMS.WPF`). You can run it using:
|
主可执行文件是 WPF 应用程序 (`DMS.WPF`)。您可以使用以下命令运行它:
|
||||||
`dotnet run --project DMS.WPF`
|
`dotnet run --project DMS.WPF`
|
||||||
|
|
||||||
**To Test:**
|
**测试:**
|
||||||
Unit tests are included in `DMS.Infrastructure.UnitTests` and `DMS.WPF.UnitTests`.
|
单元测试包含在 `DMS.Infrastructure.UnitTests` 和 `DMS.WPF.UnitTests` 中。
|
||||||
Run all tests using:
|
使用以下命令运行所有测试:
|
||||||
`dotnet test`
|
`dotnet test`
|
||||||
|
|
||||||
## Development Conventions
|
## 开发规范
|
||||||
|
|
||||||
* **Language & Framework:** C# with .NET 8.
|
* **语言与框架:** C# 配合 .NET 8。
|
||||||
* **Architecture:** Layered architecture (Core, Application, Infrastructure) with a WPF presentation layer.
|
* **架构:** 分层架构(Core, Application, Infrastructure)配合 WPF 表现层。
|
||||||
* **UI Pattern:** MVVM (Model-View-ViewModel), supported by `CommunityToolkit.Mvvm`.
|
* **UI 模式:** MVVM(Model-View-ViewModel),由 `CommunityToolkit.Mvvm` 支持。
|
||||||
* **DI (Dependency Injection):** Microsoft.Extensions.DependencyInjection is used, configured likely in the `DMS.WPF` project.
|
* **依赖注入 (DI):** 使用 Microsoft.Extensions.DependencyInjection,在 `DMS.WPF` 项目中进行配置。
|
||||||
* **Logging:** NLog is used for logging, configured via `DMS.WPF\Configurations\nlog.config`.
|
* **日志记录:** 使用 NLog 进行日志记录,配置文件位于 `DMS.WPF\Configurations\nlog.config`。
|
||||||
* **Mapping:** AutoMapper is used for object-to-object mapping.
|
* **对象映射:** 使用 AutoMapper 进行对象到对象的映射。
|
||||||
Reference in New Issue
Block a user