将NlogHelper,和NotificationHelper,改为服务的方式注入使用

This commit is contained in:
2025-09-04 17:29:24 +08:00
parent c45287fae0
commit bb650e2682
35 changed files with 307 additions and 474 deletions

View File

@@ -1,8 +1,8 @@
using System.Diagnostics;
using System.Linq.Expressions;
using DMS.Core.Helper;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
using Microsoft.Extensions.Logging;
using SqlSugar;
namespace DMS.Infrastructure.Repositories;
@@ -15,15 +15,17 @@ public abstract class BaseRepository<TEntity>
where TEntity : class, new()
{
private readonly SqlSugarDbContext _dbContext;
protected readonly ILogger<BaseRepository<TEntity>> _logger;
/// <summary>
/// 初始化 BaseRepository 的新实例。
/// </summary>
/// <param name="dbContext">SqlSugar 数据库上下文,用于数据库操作。</param>
protected BaseRepository(SqlSugarDbContext dbContext)
/// <param name="logger">日志记录器实例。</param>
protected BaseRepository(SqlSugarDbContext dbContext, ILogger<BaseRepository<TEntity>> logger)
{
_dbContext = dbContext;
_logger = logger;
}
/// <summary>
@@ -46,7 +48,7 @@ public abstract class BaseRepository<TEntity>
var result = await Db.Insertable(entity)
.ExecuteReturnEntityAsync();
stopwatch.Stop();
NlogHelper.Info($"Add {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"Add {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
@@ -62,7 +64,7 @@ public abstract class BaseRepository<TEntity>
var result = await Db.Updateable(entity)
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"Update {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"Update {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
@@ -78,7 +80,7 @@ public abstract class BaseRepository<TEntity>
var result = await Db.Deleteable(entity)
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"Delete {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"Delete {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
@@ -94,11 +96,10 @@ public abstract class BaseRepository<TEntity>
var entities = await Db.Queryable<TEntity>()
.ToListAsync();
stopwatch.Stop();
NlogHelper.Info($"GetAll {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"GetAll {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return entities;
}
/// <summary>
/// 异步根据主键 ID 获取单个实体。
/// </summary>
@@ -112,7 +113,7 @@ public abstract class BaseRepository<TEntity>
.In(id)
.FirstAsync();
stopwatch.Stop();
NlogHelper.Info($"GetById {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"GetById {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return entity;
}
@@ -129,7 +130,7 @@ public abstract class BaseRepository<TEntity>
.In(ids)
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"DeleteByIds {typeof(TEntity).Name}, Count: {ids.Count}, 耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"DeleteByIds {typeof(TEntity).Name}, Count: {ids.Count}, 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
@@ -145,7 +146,7 @@ public abstract class BaseRepository<TEntity>
var entity = await Db.Queryable<TEntity>()
.FirstAsync(expression);
stopwatch.Stop();
NlogHelper.Info($"GetByCondition {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"GetByCondition {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return entity;
}
@@ -161,7 +162,7 @@ public abstract class BaseRepository<TEntity>
var result = await Db.Queryable<TEntity>()
.AnyAsync(expression);
stopwatch.Stop();
NlogHelper.Info($"Exists {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"Exists {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
@@ -205,7 +206,7 @@ public abstract class BaseRepository<TEntity>
var entity = await Db.Queryable<TEntity>().Take(number).ToListAsync();
stopwatch.Stop();
NlogHelper.Info($"TakeAsync {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"TakeAsync {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return entity;
}
@@ -215,7 +216,7 @@ public abstract class BaseRepository<TEntity>
stopwatch.Start();
var result = await Db.Insertable(entities).ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"AddBatchAsync {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"AddBatchAsync {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return result > 0;
}
}

View File

@@ -1,10 +1,10 @@
using System.Diagnostics;
using AutoMapper;
using DMS.Core.Helper;
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;
@@ -21,8 +21,9 @@ public class DeviceRepository : BaseRepository<DbDevice>, IDeviceRepository
/// </summary>
/// <param name="mapper">AutoMapper 实例,用于实体模型和数据库模型之间的映射。</param>
/// <param name="dbContext">SqlSugar 数据库上下文,用于数据库操作。</param>
public DeviceRepository(IMapper mapper, SqlSugarDbContext dbContext)
: base(dbContext)
/// <param name="logger">日志记录器实例。</param>
public DeviceRepository(IMapper mapper, SqlSugarDbContext dbContext, ILogger<DeviceRepository> logger)
: base(dbContext, logger)
{
_mapper = mapper;
}
@@ -86,7 +87,7 @@ public class DeviceRepository : BaseRepository<DbDevice>, IDeviceRepository
var result = await Db.Deleteable(new DbDevice() { Id = id })
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"Delete {typeof(DbDevice)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"Delete {typeof(DbDevice)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}

View File

@@ -4,6 +4,7 @@ using DMS.Core.Models;
using DMS.Infrastructure.Configurations;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Entities;
using Microsoft.Extensions.Logging;
using SqlSugar;
namespace DMS.Infrastructure.Repositories;
@@ -16,15 +17,18 @@ public class InitializeRepository : IInitializeRepository
{
private readonly SqlSugarDbContext _dbContext;
private readonly SqlSugarClient _db;
private readonly ILogger<InitializeRepository> _logger;
/// <summary>
/// 构造函数,注入 SqlSugarDbContext。
/// 构造函数,注入 SqlSugarDbContext 和 ILogger
/// </summary>
/// <param name="dbContext">SqlSugar 数据库上下文,用于数据库操作。</param>
public InitializeRepository(SqlSugarDbContext dbContext)
/// <param name="logger">日志记录器实例。</param>
public InitializeRepository(SqlSugarDbContext dbContext, ILogger<InitializeRepository> logger)
{
_dbContext = dbContext;
_db = _dbContext.GetInstance();
_logger = logger;
}
/// <summary>

View File

@@ -1,11 +1,11 @@
using System.Diagnostics;
using AutoMapper;
using DMS.Core.Enums;
using DMS.Core.Helper;
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;
@@ -22,8 +22,9 @@ public class MenuRepository : BaseRepository<DbMenu>, IMenuRepository
/// </summary>
/// <param name="mapper">AutoMapper 实例,用于实体模型和数据库模型之间的映射。</param>
/// <param name="dbContext">SqlSugar 数据库上下文,用于数据库操作。</param>
public MenuRepository(IMapper mapper, SqlSugarDbContext dbContext)
: base(dbContext)
/// <param name="logger">日志记录器实例。</param>
public MenuRepository(IMapper mapper, SqlSugarDbContext dbContext, ILogger<MenuRepository> logger)
: base(dbContext, logger)
{
_mapper = mapper;
}
@@ -39,7 +40,7 @@ public class MenuRepository : BaseRepository<DbMenu>, IMenuRepository
var dbMenuTree = await Db.Queryable<DbMenu>()
.ToTreeAsync(dm => dm.Childrens, dm => dm.ParentId, 0);
stopwatch.Stop();
NlogHelper.Info($"获取菜单树耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"获取菜单树耗时:{stopwatch.ElapsedMilliseconds}ms");
return _mapper.Map<List<MenuBean>>(dbMenuTree);
}
@@ -101,7 +102,7 @@ public class MenuRepository : BaseRepository<DbMenu>, IMenuRepository
var result = await Db.Deleteable(new DbMenu { Id = id })
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
@@ -123,7 +124,7 @@ public class MenuRepository : BaseRepository<DbMenu>, IMenuRepository
.Where(m => m.Id == id)
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
return delConut;
}
@@ -147,7 +148,7 @@ public class MenuRepository : BaseRepository<DbMenu>, IMenuRepository
.Where(m => m.Id == menu.Id)
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"Delete {typeof(DbMenu)},TargetId={targetId},耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"Delete {typeof(DbMenu)},TargetId={targetId},耗时:{stopwatch.ElapsedMilliseconds}ms");
return delConut;
}

View File

@@ -1,10 +1,10 @@
using System.Diagnostics;
using AutoMapper;
using DMS.Core.Helper;
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;
@@ -21,8 +21,9 @@ public class MqttServerRepository : BaseRepository<DbMqttServer>, IMqttServerRep
/// </summary>
/// <param name="mapper">AutoMapper 实例,用于实体模型和数据库模型之间的映射。</param>
/// <param name="dbContext">SqlSugar 数据库上下文,用于数据库操作。</param>
public MqttServerRepository(IMapper mapper, SqlSugarDbContext dbContext)
: base(dbContext)
/// <param name="logger">日志记录器实例。</param>
public MqttServerRepository(IMapper mapper, SqlSugarDbContext dbContext, ILogger<MqttServerRepository> logger)
: base(dbContext, logger)
{
_mapper = mapper;
}
@@ -85,7 +86,7 @@ public class MqttServerRepository : BaseRepository<DbMqttServer>, IMqttServerRep
var result = await Db.Deleteable(new DbMqttServer() { Id = id })
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"Delete {typeof(DbMqttServer)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"Delete {typeof(DbMqttServer)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}

View File

@@ -12,32 +12,44 @@ namespace DMS.Infrastructure.Repositories;
/// </summary>
public class RepositoryManager : IRepositoryManager
{
private readonly SqlSugarClient _db;
private readonly IMapper _mapper;
private readonly SqlSugarDbContext _dbContext;
private readonly SqlSugarClient _db;
/// <summary>
/// 构造函数,注入 AutoMapper 和 SqlSugarDbContext
/// 在此初始化所有具体的仓储实例。
/// 构造函数,注入所有仓储实例
/// </summary>
/// <param name="mapper">AutoMapper 实例,用于实体模型和数据库模型之间的映射。</param>
/// <param name="dbContext">SqlSugar 数据库上下文,用于数据库操作。</param>
public RepositoryManager(IMapper mapper, SqlSugarDbContext dbContext)
/// <param name="initializeRepository">初始化仓储实例。</param>
/// <param name="devices">设备仓储实例。</param>
/// <param name="variableTables">变量表仓储实例。</param>
/// <param name="variables">变量仓储实例。</param>
/// <param name="mqttServers">MQTT服务器仓储实例。</param>
/// <param name="variableMqttAliases">变量MQTT别名仓储实例。</param>
/// <param name="menus">菜单仓储实例。</param>
/// <param name="variableHistories">变量历史仓储实例。</param>
/// <param name="users">用户仓储实例。</param>
public RepositoryManager( SqlSugarDbContext dbContext,
IInitializeRepository initializeRepository,
IDeviceRepository devices,
IVariableTableRepository variableTables,
IVariableRepository variables,
IMqttServerRepository mqttServers,
IVariableMqttAliasRepository variableMqttAliases,
IMenuRepository menus,
IVariableHistoryRepository variableHistories,
IUserRepository users)
{
_mapper = mapper;
_dbContext = dbContext;
_db = dbContext.GetInstance();
// 初始化各个仓储实例
InitializeRepository=new InitializeRepository(dbContext);
Devices = new DeviceRepository(mapper, dbContext);
VariableTables = new VariableTableRepository(mapper, dbContext);
Variables = new VariableRepository(mapper, dbContext);
MqttServers = new MqttServerRepository(mapper, dbContext);
VariableMqttAliases = new VariableMqttAliasRepository(mapper, dbContext);
Menus = new MenuRepository(mapper, dbContext);
VariableHistories = new VariableHistoryRepository(mapper, dbContext);
Users = new UserRepository(mapper, dbContext);
InitializeRepository = initializeRepository;
Devices = devices;
VariableTables = variableTables;
Variables = variables;
MqttServers = mqttServers;
VariableMqttAliases = variableMqttAliases;
Menus = menus;
VariableHistories = variableHistories;
Users = users;
_db = dbContext.GetInstance();
}
/// <summary>
@@ -45,7 +57,7 @@ public class RepositoryManager : IRepositoryManager
/// </summary>
public void Dispose()
{
_db.Close();
_db?.Close();
}
/// <summary>
@@ -89,17 +101,29 @@ public class RepositoryManager : IRepositoryManager
/// 异步开始数据库事务。
/// </summary>
/// <returns>表示异步操作的任务。</returns>
public async Task BeginTranAsync() => await _db.BeginTranAsync();
public async Task BeginTranAsync()
{
if (_db != null)
await _db.BeginTranAsync();
}
/// <summary>
/// 异步提交数据库事务。
/// </summary>
/// <returns>表示异步操作的任务。</returns>
public async Task CommitAsync() => await _db.CommitTranAsync();
public async Task CommitAsync()
{
if (_db != null)
await _db.CommitTranAsync();
}
/// <summary>
/// 异步回滚数据库事务。
/// </summary>
/// <returns>表示异步操作的任务。</returns>
public async Task RollbackAsync() => await _db.RollbackTranAsync();
public async Task RollbackAsync()
{
if (_db != null)
await _db.RollbackTranAsync();
}
}

View File

@@ -7,7 +7,7 @@ using DMS.Infrastructure.Entities;
using AutoMapper;
using DMS.Core.Helper;
using Microsoft.Extensions.Logging;
namespace DMS.Infrastructure.Repositories;
@@ -24,8 +24,9 @@ public class UserRepository : BaseRepository<DbUser>, IUserRepository
/// </summary>
/// <param name="mapper">AutoMapper 实例,用于实体模型和数据库模型之间的映射。</param>
/// <param name="dbContext">SqlSugar 数据库上下文,用于数据库操作。</param>
public UserRepository(IMapper mapper, SqlSugarDbContext dbContext)
: base(dbContext)
/// <param name="logger">日志记录器实例。</param>
public UserRepository(IMapper mapper, SqlSugarDbContext dbContext, ILogger<UserRepository> logger)
: base(dbContext, logger)
{
_mapper = mapper;
}
@@ -88,7 +89,7 @@ public class UserRepository : BaseRepository<DbUser>, IUserRepository
var result = await Db.Deleteable(new DbUser() { Id = id })
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"Delete {typeof(DbUser)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"Delete {typeof(DbUser)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}

View File

@@ -6,7 +6,7 @@ using DMS.Infrastructure.Entities;
using AutoMapper;
using DMS.Core.Helper;
using Microsoft.Extensions.Logging;
namespace DMS.Infrastructure.Repositories;
@@ -23,8 +23,9 @@ public class VariableHistoryRepository : BaseRepository<DbVariableHistory>, IVar
/// </summary>
/// <param name="mapper">AutoMapper 实例,用于实体模型和数据库模型之间的映射。</param>
/// <param name="dbContext">SqlSugar 数据库上下文,用于数据库操作。</param>
public VariableHistoryRepository(IMapper mapper, SqlSugarDbContext dbContext)
: base(dbContext)
/// <param name="logger">日志记录器实例。</param>
public VariableHistoryRepository(IMapper mapper, SqlSugarDbContext dbContext, ILogger<VariableHistoryRepository> logger)
: base(dbContext, logger)
{
_mapper = mapper;
}
@@ -87,7 +88,7 @@ public class VariableHistoryRepository : BaseRepository<DbVariableHistory>, IVar
var result = await Db.Deleteable(new DbVariableHistory() { Id = id })
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"Delete {typeof(DbVariableHistory)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"Delete {typeof(DbVariableHistory)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}

View File

@@ -5,7 +5,7 @@ using DMS.Infrastructure.Entities;
using AutoMapper;
using DMS.Core.Helper;
using Microsoft.Extensions.Logging;
using DMS.Core.Models;
namespace DMS.Infrastructure.Repositories;
@@ -23,8 +23,9 @@ public class VariableMqttAliasRepository : BaseRepository<DbVariableMqttAlias>,
/// </summary>
/// <param name="mapper">AutoMapper 实例,用于实体模型和数据库模型之间的映射。</param>
/// <param name="dbContext">SqlSugar 数据库上下文,用于数据库操作。</param>
public VariableMqttAliasRepository(IMapper mapper, SqlSugarDbContext dbContext)
: base(dbContext)
/// <param name="logger">日志记录器实例。</param>
public VariableMqttAliasRepository(IMapper mapper, SqlSugarDbContext dbContext, ILogger<VariableMqttAliasRepository> logger)
: base(dbContext, logger)
{
_mapper = mapper;
}
@@ -87,7 +88,7 @@ public class VariableMqttAliasRepository : BaseRepository<DbVariableMqttAlias>,
var result = await Db.Deleteable(new DbVariableMqttAlias() { Id = id })
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"Delete {typeof(DbVariableMqttAlias)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"Delete {typeof(DbVariableMqttAlias)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}

View File

@@ -1,10 +1,10 @@
using System.Diagnostics;
using AutoMapper;
using DMS.Core.Helper;
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;
@@ -21,8 +21,9 @@ public class VariableRepository : BaseRepository<DbVariable>, IVariableRepositor
/// </summary>
/// <param name="mapper">AutoMapper 实例,用于实体模型和数据库模型之间的映射。</param>
/// <param name="dbContext">SqlSugar 数据库上下文,用于数据库操作。</param>
public VariableRepository(IMapper mapper, SqlSugarDbContext dbContext)
: base(dbContext)
/// <param name="logger">日志记录器实例。</param>
public VariableRepository(IMapper mapper, SqlSugarDbContext dbContext, ILogger<VariableRepository> logger)
: base(dbContext, logger)
{
_mapper = mapper;
}
@@ -98,13 +99,13 @@ public class VariableRepository : BaseRepository<DbVariable>, IVariableRepositor
}
await Db.CommitTranAsync();
//NlogHelper.Info($"成功为 {variableMqttList.Count()} 个变量请求添加/更新了MQTT服务器关联实际影响 {affectedCount} 个。");
//_logger.LogInformation($"成功为 {variableMqttList.Count()} 个变量请求添加/更新了MQTT服务器关联实际影响 {affectedCount} 个。");
return affectedCount;
}
catch (Exception ex)
{
await Db.RollbackTranAsync();
//NlogHelper.Error($"为变量添加MQTT服务器关联时发生错误: {ex.Message}", ex);
//_logger.LogError(ex, $"为变量添加MQTT服务器关联时发生错误: {ex.Message}");
// 根据需要,可以向上层抛出异常
throw;
}
@@ -169,7 +170,7 @@ public class VariableRepository : BaseRepository<DbVariable>, IVariableRepositor
var result = await Db.Deleteable(new DbVariable() { Id = id })
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"Delete {typeof(DbVariable)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"Delete {typeof(DbVariable)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
@@ -186,7 +187,7 @@ public class VariableRepository : BaseRepository<DbVariable>, IVariableRepositor
.In(ids)
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"Delete {typeof(DbVariable)},Count={ids.Count},耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"Delete {typeof(DbVariable)},Count={ids.Count},耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}

View File

@@ -6,7 +6,7 @@ using DMS.Infrastructure.Entities;
using AutoMapper;
using DMS.Core.Helper;
using Microsoft.Extensions.Logging;
namespace DMS.Infrastructure.Repositories;
@@ -23,8 +23,9 @@ public class VariableTableRepository : BaseRepository<DbVariableTable>, IVariabl
/// </summary>
/// <param name="mapper">AutoMapper 实例,用于实体模型和数据库模型之间的映射。</param>
/// <param name="dbContext">SqlSugar 数据库上下文,用于数据库操作。</param>
public VariableTableRepository(IMapper mapper, SqlSugarDbContext dbContext)
: base(dbContext)
/// <param name="logger">日志记录器实例。</param>
public VariableTableRepository(IMapper mapper, SqlSugarDbContext dbContext, ILogger<VariableTableRepository> logger)
: base(dbContext, logger)
{
_mapper = mapper;
}
@@ -87,7 +88,7 @@ public class VariableTableRepository : BaseRepository<DbVariableTable>, IVariabl
var result = await Db.Deleteable(new DbVariableTable() { Id = id })
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"Delete {typeof(DbVariableTable)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"Delete {typeof(DbVariableTable)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
@@ -122,7 +123,7 @@ public class VariableTableRepository : BaseRepository<DbVariableTable>, IVariabl
.Where(it => it.DeviceId == deviceId)
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"Delete VariableTable by DeviceId={deviceId}, 耗时:{stopwatch.ElapsedMilliseconds}ms");
_logger.LogInformation($"Delete VariableTable by DeviceId={deviceId}, 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
}