diff --git a/Data/Repositories/DeviceRepository.cs b/Data/Repositories/DeviceRepository.cs index 4785627..478bbb4 100644 --- a/Data/Repositories/DeviceRepository.cs +++ b/Data/Repositories/DeviceRepository.cs @@ -1,6 +1,5 @@ using System.Diagnostics; using iNKORE.UI.WPF.Modern.Common.IconKeys; -using NLog; using PMSWPF.Data.Entities; using PMSWPF.Enums; using PMSWPF.Extensions; @@ -14,7 +13,6 @@ public class DeviceRepository { private readonly MenuRepository _menuRepository; private readonly VarTableRepository _varTableRepository; - private static readonly ILogger Logger = LogManager.GetCurrentClassLogger(); public DeviceRepository() { @@ -36,7 +34,7 @@ public class DeviceRepository { var result = await Edit(device, db); stopwatch.Stop(); - Logger.Info($"编辑设备 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"编辑设备 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } @@ -53,7 +51,7 @@ public class DeviceRepository var result = await db.Updateable(device.CopyTo()) .ExecuteCommandAsync(); stopwatch.Stop(); - Logger.Info($"编辑设备 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"编辑设备 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } @@ -80,7 +78,7 @@ public class DeviceRepository } stopwatch.Stop(); - Logger.Info($"加载设备列表总耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"加载设备列表总耗时:{stopwatch.ElapsedMilliseconds}ms"); return devices; } @@ -100,7 +98,7 @@ public class DeviceRepository var result = await db.Queryable() .FirstAsync(p => p.Id == id); stopwatch.Stop(); - Logger.Info($"根据ID '{id}' 获取设备耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"根据ID '{id}' 获取设备耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } @@ -123,7 +121,7 @@ public class DeviceRepository var res = await Delete(device, menus, db); stopwatch.Stop(); - Logger.Info($"删除设备:{device.Name},耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"删除设备:{device.Name},耗时:{stopwatch.ElapsedMilliseconds}ms"); await db.CommitTranAsync(); return res; } @@ -161,7 +159,7 @@ public class DeviceRepository throw new NullReferenceException($"没有找到设备:{device.Name},的菜单对象。"); await _menuRepository.DeleteMenu(menu, db); stopwatch.Stop(); - Logger.Info($"删除设备:{device.Name},耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"删除设备:{device.Name},耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } @@ -206,7 +204,7 @@ public class DeviceRepository finally { stopwatch.Stop(); - Logger.Info($"添加设备 '{device.Name}' 及相关菜单耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"添加设备 '{device.Name}' 及相关菜单耗时:{stopwatch.ElapsedMilliseconds}ms"); } } diff --git a/Data/Repositories/MenuRepository.cs b/Data/Repositories/MenuRepository.cs index b94167a..5b212e0 100644 --- a/Data/Repositories/MenuRepository.cs +++ b/Data/Repositories/MenuRepository.cs @@ -1,17 +1,18 @@ using System.Diagnostics; using iNKORE.UI.WPF.Modern.Common.IconKeys; -using NLog; using PMSWPF.Data.Entities; using PMSWPF.Enums; using PMSWPF.Extensions; +using PMSWPF.Helper; using PMSWPF.Models; using SqlSugar; +using PMSWPF.Helper; + namespace PMSWPF.Data.Repositories; public class MenuRepository { - private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); public MenuRepository() { @@ -36,7 +37,7 @@ public class MenuRepository var result = await db.Deleteable(childList) .ExecuteCommandAsync(); stopwatch.Stop(); - Logger.Info($"删除菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"删除菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } @@ -53,7 +54,7 @@ public class MenuRepository foreach (var dbMenu in dbMenuTree) menuTree.Add(dbMenu.CopyTo()); stopwatch.Stop(); - Logger.Info($"获取菜单树耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"获取菜单树耗时:{stopwatch.ElapsedMilliseconds}ms"); return menuTree; } } @@ -66,7 +67,7 @@ public class MenuRepository using var db = DbContext.GetInstance(); var result = await Add(menu, db); stopwatch.Stop(); - Logger.Info($"添加菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"添加菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } @@ -84,7 +85,7 @@ public class MenuRepository var result = await db.Insertable(menu.CopyTo()) .ExecuteCommandAsync(); stopwatch.Stop(); - Logger.Info($"添加菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"添加菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } @@ -104,7 +105,7 @@ public class MenuRepository var addTableRes = await db.Insertable(addVarTable) .ExecuteCommandAsync(); stopwatch.Stop(); - // Logger.Info($"添加变量表菜单 '{addVarTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + // NlogHelper.Info($"添加变量表菜单 '{addVarTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return addTableRes; } @@ -137,7 +138,7 @@ public class MenuRepository var addDeviceMenuId = await db.Insertable(menu.CopyTo()) .ExecuteReturnIdentityAsync(); stopwatch.Stop(); - Logger.Info($"添加设备菜单 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"添加设备菜单 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return addDeviceMenuId; } @@ -154,7 +155,7 @@ public class MenuRepository { var result = await Edit(menu, db); stopwatch.Stop(); - Logger.Info($"编辑菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"编辑菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } @@ -171,7 +172,7 @@ public class MenuRepository var result = await db.Updateable(menu.CopyTo()) .ExecuteCommandAsync(); stopwatch.Stop(); - Logger.Info($"编辑菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"编辑菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } @@ -184,7 +185,7 @@ public class MenuRepository var result = await db.Queryable() .FirstAsync(m => m.DataId == dataId && m.Type == menuType); stopwatch.Stop(); - Logger.Info($"根据DataId '{dataId}' 和 MenuType '{menuType}' 获取菜单耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"根据DataId '{dataId}' 和 MenuType '{menuType}' 获取菜单耗时:{stopwatch.ElapsedMilliseconds}ms"); return result?.CopyTo(); } } diff --git a/Data/Repositories/MqttRepository.cs b/Data/Repositories/MqttRepository.cs index ba3b355..eac7d3c 100644 --- a/Data/Repositories/MqttRepository.cs +++ b/Data/Repositories/MqttRepository.cs @@ -1,7 +1,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.Threading.Tasks; -using NLog; using PMSWPF.Data.Entities; using PMSWPF.Models; using PMSWPF.Extensions; @@ -18,7 +17,6 @@ namespace PMSWPF.Data.Repositories; public class MqttRepository { private readonly MenuRepository _menuRepository; - private static readonly ILogger Logger = LogManager.GetCurrentClassLogger(); public MqttRepository() { @@ -40,7 +38,7 @@ public class MqttRepository .In(id) .SingleAsync(); stopwatch.Stop(); - Logger.Info($"根据ID '{id}' 获取Mqtt配置耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"根据ID '{id}' 获取Mqtt配置耗时:{stopwatch.ElapsedMilliseconds}ms"); return result.CopyTo(); } } @@ -58,7 +56,7 @@ public class MqttRepository var result = await _db.Queryable().Includes(m=>m.VariableDatas) .ToListAsync(); stopwatch.Stop(); - Logger.Info($"获取所有Mqtt配置耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"获取所有Mqtt配置耗时:{stopwatch.ElapsedMilliseconds}ms"); return result.Select(m => m.CopyTo()) .ToList(); } @@ -92,13 +90,13 @@ public class MqttRepository await _menuRepository.Add(menu, db); await db.CommitTranAsync(); stopwatch.Stop(); - Logger.Info($"新增Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"新增Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } catch (Exception ex) { await db.RollbackTranAsync(); - Logger.Error(ex, $"添加MQTT配置 {{mqtt.Name}} 失败"); + NlogHelper.Error( $"添加MQTT配置 {{mqtt.Name}} 失败",ex); throw; } } @@ -129,13 +127,13 @@ public class MqttRepository await db.CommitTranAsync(); stopwatch.Stop(); - Logger.Info($"更新Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"更新Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } catch (Exception ex) { await db.RollbackTranAsync(); - Logger.Error(ex, $"更新MQTT配置 {{mqtt.Name}} 失败"); + NlogHelper.Error($"更新MQTT配置 {{mqtt.Name}} 失败", ex); throw; } } @@ -163,13 +161,13 @@ public class MqttRepository await _menuRepository.DeleteMenu(menu, db); await db.CommitTranAsync(); stopwatch.Stop(); - Logger.Info($"删除Mqtt配置ID '{mqtt.Id}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"删除Mqtt配置ID '{mqtt.Id}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } catch (Exception ex) { await db.RollbackTranAsync(); - Logger.Error(ex, $"删除MQTT配置 {{mqtt.Name}} 失败"); + NlogHelper.Error( $"删除MQTT配置 {{mqtt.Name}} 失败",ex); throw; } } diff --git a/Data/Repositories/UserRepository.cs b/Data/Repositories/UserRepository.cs index 86b030b..15fc79d 100644 --- a/Data/Repositories/UserRepository.cs +++ b/Data/Repositories/UserRepository.cs @@ -1,8 +1,8 @@ using System.Collections.Generic; using System.Diagnostics; using System.Threading.Tasks; -using NLog; using PMSWPF.Data.Entities; +using PMSWPF.Helper; namespace PMSWPF.Data.Repositories; @@ -11,7 +11,6 @@ namespace PMSWPF.Data.Repositories; /// public class UserRepository { - private static readonly ILogger Logger = LogManager.GetCurrentClassLogger(); /// /// 根据ID获取用户 @@ -26,7 +25,7 @@ public class UserRepository { var result = await _db.Queryable().In(id).SingleAsync(); stopwatch.Stop(); - Logger.Info($"根据ID '{id}' 获取用户耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"根据ID '{id}' 获取用户耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } @@ -43,7 +42,7 @@ public class UserRepository { var result = await _db.Queryable().ToListAsync(); stopwatch.Stop(); - Logger.Info($"获取所有用户耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"获取所有用户耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } @@ -61,7 +60,7 @@ public class UserRepository { var result = await _db.Insertable(user).ExecuteReturnIdentityAsync(); stopwatch.Stop(); - Logger.Info($"新增用户 '{user.Id}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"新增用户 '{user.Id}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } @@ -79,7 +78,7 @@ public class UserRepository { var result = await _db.Updateable(user).ExecuteCommandAsync(); stopwatch.Stop(); - Logger.Info($"更新用户 '{user.Id}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"更新用户 '{user.Id}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } @@ -97,7 +96,7 @@ public class UserRepository { var result = await _db.Deleteable().In(id).ExecuteCommandAsync(); stopwatch.Stop(); - Logger.Info($"删除用户ID '{id}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"删除用户ID '{id}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } diff --git a/Data/Repositories/VarDataRepository.cs b/Data/Repositories/VarDataRepository.cs index aad5bd5..7403905 100644 --- a/Data/Repositories/VarDataRepository.cs +++ b/Data/Repositories/VarDataRepository.cs @@ -1,9 +1,9 @@ using System.Collections.Generic; using System.Diagnostics; using System.Threading.Tasks; -using NLog; using PMSWPF.Data.Entities; using PMSWPF.Extensions; +using PMSWPF.Helper; using PMSWPF.Models; using SqlSugar; @@ -14,7 +14,6 @@ namespace PMSWPF.Data.Repositories; /// public class VarDataRepository { - private static readonly ILogger Logger = LogManager.GetCurrentClassLogger(); /// /// 根据ID获取VariableData @@ -31,7 +30,7 @@ public class VarDataRepository .In(id) .SingleAsync(); stopwatch.Stop(); - Logger.Info($"根据ID '{id}' 获取VariableData耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"根据ID '{id}' 获取VariableData耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } @@ -49,7 +48,7 @@ public class VarDataRepository var result = await _db.Queryable() .ToListAsync(); stopwatch.Stop(); - Logger.Info($"获取所有VariableData耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"获取所有VariableData耗时:{stopwatch.ElapsedMilliseconds}ms"); return result.Select(d => d.CopyTo()) .ToList(); } @@ -70,7 +69,7 @@ public class VarDataRepository .Select(dbVarData => dbVarData.CopyTo()) .ToListAsync(); stopwatch.Stop(); - Logger.Info($"获取变量表的所有变量{result.Count()}个耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"获取变量表的所有变量{result.Count()}个耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } @@ -88,7 +87,7 @@ public class VarDataRepository { var varData = await AddAsync(variableData, db); stopwatch.Stop(); - Logger.Info($"新增VariableData '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"新增VariableData '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return varData; } } @@ -106,7 +105,7 @@ public class VarDataRepository var dbVarData = await db.Insertable(variableData.CopyTo()) .ExecuteReturnEntityAsync(); stopwatch.Stop(); - Logger.Info($"新增VariableData '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"新增VariableData '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return dbVarData.CopyTo(); } @@ -123,7 +122,7 @@ public class VarDataRepository { var varData = await AddAsync(variableDatas, db); stopwatch.Stop(); - Logger.Info($"新增VariableData '{variableDatas.Count()}'个, 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"新增VariableData '{variableDatas.Count()}'个, 耗时:{stopwatch.ElapsedMilliseconds}ms"); return varData; } } @@ -143,13 +142,13 @@ public class VarDataRepository var dbList = variableDatas.Select(vb => vb.CopyTo()) .ToList(); stopwatch2.Stop(); - Logger.Info($"复制 VariableData'{variableDatas.Count()}'个, 耗时:{stopwatch2.ElapsedMilliseconds}ms"); + NlogHelper.Info($"复制 VariableData'{variableDatas.Count()}'个, 耗时:{stopwatch2.ElapsedMilliseconds}ms"); var res = await db.Insertable(dbList) .ExecuteCommandAsync(); stopwatch.Stop(); - Logger.Info($"新增VariableData '{variableDatas.Count()}'个, 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"新增VariableData '{variableDatas.Count()}'个, 耗时:{stopwatch.ElapsedMilliseconds}ms"); return res; } @@ -169,7 +168,7 @@ public class VarDataRepository .Include(d => d.Mqtts) .ExecuteCommandAsync(); stopwatch.Stop(); - Logger.Info($"更新VariableData '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"更新VariableData '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } @@ -188,7 +187,7 @@ public class VarDataRepository var result = await UpdateAsync(variableDatas, _db); stopwatch.Stop(); - Logger.Info($"更新VariableData {variableDatas.Count()}个 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"更新VariableData {variableDatas.Count()}个 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } @@ -209,7 +208,7 @@ public class VarDataRepository .ExecuteCommandAsync(); stopwatch.Stop(); - Logger.Info($"更新VariableData {variableDatas.Count()}个 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"更新VariableData {variableDatas.Count()}个 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } @@ -225,7 +224,7 @@ public class VarDataRepository using var db = DbContext.GetInstance(); var result = await DeleteAsync(variableData, db); stopwatch.Stop(); - Logger.Info($"删除VariableData: '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"删除VariableData: '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } @@ -244,7 +243,7 @@ public class VarDataRepository .Where(d => d.Id == variableData.Id) .ExecuteCommandAsync(); stopwatch.Stop(); - Logger.Info($"删除VariableData: '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"删除VariableData: '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } @@ -262,7 +261,7 @@ public class VarDataRepository var result = await DeleteAsync(variableDatas, db); stopwatch.Stop(); - Logger.Info($"删除VariableData: '{variableDatas.Count()}'个 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"删除VariableData: '{variableDatas.Count()}'个 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } @@ -282,7 +281,7 @@ public class VarDataRepository var result = await _db.Deleteable(dbList) .ExecuteCommandAsync(); stopwatch.Stop(); - Logger.Info($"删除VariableData: '{variableDatas.Count()}'个 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"删除VariableData: '{variableDatas.Count()}'个 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } \ No newline at end of file diff --git a/Data/Repositories/VarTableRepository.cs b/Data/Repositories/VarTableRepository.cs index 66bc067..6347341 100644 --- a/Data/Repositories/VarTableRepository.cs +++ b/Data/Repositories/VarTableRepository.cs @@ -2,16 +2,15 @@ using iNKORE.UI.WPF.Modern.Common.IconKeys; using PMSWPF.Data.Entities; using PMSWPF.Enums; using PMSWPF.Extensions; +using PMSWPF.Helper; using PMSWPF.Models; using SqlSugar; using System.Diagnostics; -using NLog; namespace PMSWPF.Data.Repositories; public class VarTableRepository { - private static readonly ILogger Logger = LogManager.GetCurrentClassLogger(); /// /// 添加变量表 @@ -27,7 +26,7 @@ public class VarTableRepository var addVarTable = await Add(varTable, db); stopwatch.Stop(); - Logger.Info($"添加变量表 '{varTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"添加变量表 '{varTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return addVarTable; } } @@ -47,7 +46,7 @@ public class VarTableRepository var addVarTabel = await db.Insertable(variableTable.CopyTo()) .ExecuteReturnEntityAsync(); stopwatch.Stop(); - Logger.Info($"添加设备 '{addVarTabel.Name}' 的默认变量表耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"添加设备 '{addVarTabel.Name}' 的默认变量表耗时:{stopwatch.ElapsedMilliseconds}ms"); return addVarTabel.CopyTo(); } @@ -64,7 +63,7 @@ public class VarTableRepository { var result = await Edit(variableTable, db); stopwatch.Stop(); - Logger.Info($"编辑变量表 '{variableTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"编辑变量表 '{variableTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } @@ -81,7 +80,7 @@ public class VarTableRepository var result = await db.Updateable(variableTable.CopyTo()) .ExecuteCommandAsync(); stopwatch.Stop(); - Logger.Info($"编辑变量表 '{variableTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"编辑变量表 '{variableTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } @@ -98,7 +97,7 @@ public class VarTableRepository { var result = await Delete(variableTable, db); stopwatch.Stop(); - Logger.Info($"删除变量表 '{variableTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"删除变量表 '{variableTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } @@ -118,7 +117,7 @@ public class VarTableRepository var res= await db.Deleteable(varTable.CopyTo()) .ExecuteCommandAsync(); stopwatch.Stop(); - Logger.Info($"删除变量表 '{varTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Info($"删除变量表 '{varTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return res; } diff --git a/Services/DataServices.cs b/Services/DataServices.cs index 7ca9f82..801da7d 100644 --- a/Services/DataServices.cs +++ b/Services/DataServices.cs @@ -18,34 +18,47 @@ namespace PMSWPF.Services; /// public partial class DataServices : ObservableRecipient, IRecipient { - // 日志记录器,用于记录数据服务中的操作和错误。 - private readonly ILogger _logger; // 设备列表,使用ObservableProperty特性,当值改变时会自动触发属性变更通知。 - [ObservableProperty] private List _devices; + [ObservableProperty] + private List _devices; + // 变量表列表。 - [ObservableProperty] private List _variableTables; + [ObservableProperty] + private List _variableTables; + // 变量数据列表。 - [ObservableProperty] private List _variableDatas; + [ObservableProperty] + private List _variableDatas; + // 菜单树列表。 - [ObservableProperty] private List menuTrees; + [ObservableProperty] + private List menuTrees; + // MQTT配置列表。 - [ObservableProperty] private List _mqtts; + [ObservableProperty] + private List _mqtts; // 设备数据仓库,用于设备数据的CRUD操作。 private readonly DeviceRepository _deviceRepository; + // 菜单数据仓库,用于菜单数据的CRUD操作。 private readonly MenuRepository _menuRepository; + // MQTT数据仓库,用于MQTT配置数据的CRUD操作。 private readonly MqttRepository _mqttRepository; + // 变量数据仓库,用于变量数据的CRUD操作。 private readonly VarDataRepository _varDataRepository; // 设备列表变更事件,当设备列表数据更新时触发。 public event EventHandler> OnDeviceListChanged; + // 菜单树列表变更事件,当菜单树数据更新时触发。 public event EventHandler> OnMenuTreeListChanged; + // MQTT列表变更事件,当MQTT配置数据更新时触发。 public event EventHandler> OnMqttListChanged; + // 变量数据变更事件,当变量数据更新时触发。 public event EventHandler> OnVariableDataChanged; @@ -86,9 +99,8 @@ public partial class DataServices : ObservableRecipient, IRecipient /// 注入ILogger,并初始化各个数据仓库。 /// /// 日志记录器实例。 - public DataServices(ILogger logger) + public DataServices() { - _logger = logger; IsActive = true; // 激活消息接收器 _deviceRepository = new DeviceRepository(); _menuRepository = new MenuRepository(); diff --git a/Services/MqttBackgroundService.cs b/Services/MqttBackgroundService.cs index 2a8115e..6037240 100644 --- a/Services/MqttBackgroundService.cs +++ b/Services/MqttBackgroundService.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; -using NLog; using MQTTnet; using MQTTnet.Client; using MQTTnet.Client.Options; @@ -20,8 +19,6 @@ namespace PMSWPF.Services /// public class MqttBackgroundService : BackgroundService { - // NLog日志记录器,用于记录服务运行时的信息、警告和错误。 - private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); // 数据服务实例,用于访问和操作应用程序数据,如MQTT配置和变量数据。 private readonly DataServices _dataServices; // 存储MQTT客户端实例的字典,键为MQTT配置ID,值为IMqttClient对象。 @@ -52,7 +49,7 @@ namespace PMSWPF.Services /// 表示异步操作的任务。 protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - Logger.Info("MqttBackgroundService started."); // 记录服务启动信息 + NlogHelper.Info("MqttBackgroundService started."); // 记录服务启动信息 // 订阅MQTT列表和变量数据变化的事件,以便在数据更新时重新加载配置和数据。 _dataServices.OnMqttListChanged += HandleMqttListChanged; @@ -76,7 +73,7 @@ namespace PMSWPF.Services /// 表示异步操作的任务。 public override async Task StopAsync(CancellationToken stoppingToken) { - Logger.Info("MqttBackgroundService stopping."); // 记录服务停止信息 + NlogHelper.Info("MqttBackgroundService stopping."); // 记录服务停止信息 // 停止定时器。 _timer?.Change(Timeout.Infinite, 0); @@ -132,7 +129,7 @@ namespace PMSWPF.Services // 发布MQTT消息。 await client.PublishAsync(message); - Logger.Info($"Published {variable.Name} = {variable.DataValue} to {topic}/{variable.Name}"); // 记录发布信息 + NlogHelper.Info($"Published {variable.Name} = {variable.DataValue} to {topic}/{variable.Name}"); // 记录发布信息 variable.IsModified = false; // 发布后重置修改标志。 } } @@ -172,7 +169,7 @@ namespace PMSWPF.Services await client.DisconnectAsync(); } _mqttClients.Remove(id); - Logger.Info($"Disconnected and removed MQTT client for ID: {id}"); + NlogHelper.Info($"Disconnected and removed MQTT client for ID: {id}"); } _mqttConfigurations.Remove(id); _mqttVariableData.Remove(id); @@ -202,14 +199,14 @@ namespace PMSWPF.Services // 设置连接成功事件处理程序。 client.UseConnectedHandler(e => { - Logger.Info($"Connected to MQTT broker: {mqtt.Name}"); + NlogHelper.Info($"Connected to MQTT broker: {mqtt.Name}"); NotificationHelper.ShowSuccess($"已连接到MQTT服务器: {mqtt.Name}"); }); // 设置断开连接事件处理程序。 client.UseDisconnectedHandler(async e => { - Logger.Warn($"Disconnected from MQTT broker: {mqtt.Name}. Reason: {e.Reason}"); + NlogHelper.Warn($"Disconnected from MQTT broker: {mqtt.Name}. Reason: {e.Reason}"); NotificationHelper.ShowInfo($"与MQTT服务器断开连接: {mqtt.Name}"); // 尝试重新连接。 await Task.Delay(TimeSpan.FromSeconds(5)); // 等待5秒后重连 @@ -219,7 +216,7 @@ namespace PMSWPF.Services } catch (Exception ex) { - Logger.Error(ex, $"Failed to reconnect to MQTT broker: {mqtt.Name}"); + NlogHelper.Error($"Failed to reconnect to MQTT broker: {mqtt.Name}",ex ); } }); @@ -230,7 +227,6 @@ namespace PMSWPF.Services } catch (Exception ex) { - Logger.Error(ex, $"Failed to connect to MQTT broker: {mqtt.Name}"); NotificationHelper.ShowError($"连接MQTT服务器失败: {mqtt.Name} - {ex.Message}", ex); } } @@ -271,7 +267,7 @@ namespace PMSWPF.Services /// 更新后的MQTT配置列表。 private async void HandleMqttListChanged(object sender, List mqtts) { - Logger.Info("MQTT list changed. Reloading configurations."); // 记录MQTT列表变化信息 + NlogHelper.Info("MQTT list changed. Reloading configurations."); // 记录MQTT列表变化信息 // 重新加载MQTT配置和变量数据。 await LoadMqttConfigurations(); await LoadVariableData(); // 重新加载变量数据,以防关联发生变化 @@ -284,7 +280,7 @@ namespace PMSWPF.Services /// 更新后的变量数据列表。 private async void HandleVariableDataChanged(object sender, List variableDatas) { - Logger.Info("Variable data changed. Reloading variable associations."); // 记录变量数据变化信息 + NlogHelper.Info("Variable data changed. Reloading variable associations."); // 记录变量数据变化信息 // 重新加载变量数据。 await LoadVariableData(); } diff --git a/Services/NavgatorServices.cs b/Services/NavgatorServices.cs index 455a85b..d1560b0 100644 --- a/Services/NavgatorServices.cs +++ b/Services/NavgatorServices.cs @@ -11,14 +11,11 @@ namespace PMSWPF.Services; public partial class NavgatorServices : ObservableRecipient, IRecipient { - private readonly ILogger _logger; - // [ObservableProperty] private ViewModelBase currentViewModel; - public NavgatorServices(ILogger logger) + public NavgatorServices() { - _logger = logger; IsActive = true; } diff --git a/Services/S7BackgroundService.cs b/Services/S7BackgroundService.cs index f44deb9..1b074ae 100644 --- a/Services/S7BackgroundService.cs +++ b/Services/S7BackgroundService.cs @@ -18,19 +18,21 @@ namespace PMSWPF.Services /// public class S7BackgroundService : BackgroundService { - // 日志记录器,用于记录服务运行时的信息、警告和错误。 - private readonly ILogger _logger; // 数据服务实例,用于访问和操作应用程序数据,如设备配置。 private readonly DataServices _dataServices; + // 存储S7 PLC客户端实例的字典,键为设备ID,值为Plc对象。 private readonly Dictionary _s7PlcClients = new Dictionary(); + // 轮询数据的线程。 private Thread _pollingThread; + // 用于取消轮询操作的CancellationTokenSource。 private CancellationTokenSource _cancellationTokenSource; // 读取变量计数器。 private int readCount = 0; + // 跳过变量计数器(未到轮询时间)。 private int TGCount = 0; @@ -65,9 +67,8 @@ namespace PMSWPF.Services /// /// 日志记录器实例。 /// 数据服务实例。 - public S7BackgroundService(ILogger logger, DataServices dataServices) + public S7BackgroundService(DataServices dataServices) { - _logger = logger; _dataServices = dataServices; // 订阅设备列表变更事件,以便在设备配置更新时重新加载。 _dataServices.OnDeviceListChanged += HandleDeviceListChanged; @@ -86,7 +87,7 @@ namespace PMSWPF.Services // 当设备列表变化时,更新PLC客户端 // 这里需要更复杂的逻辑来处理连接的关闭和新连接的建立 // 简单起见,这里只做日志记录 - _logger.LogInformation("设备列表已更改。S7客户端可能需要重新初始化。"); + NlogHelper.Info("设备列表已更改。S7客户端可能需要重新初始化。"); } /// @@ -96,7 +97,7 @@ namespace PMSWPF.Services /// 表示异步操作的任务。 protected override Task ExecuteAsync(CancellationToken stoppingToken) { - _logger.LogInformation("S7后台服务正在启动。"); + NlogHelper.Info("S7后台服务正在启动。"); // 创建一个CancellationTokenSource,用于控制轮询线程的取消。 _cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken); @@ -116,9 +117,9 @@ namespace PMSWPF.Services /// 用于取消轮询的CancellationToken。 private void PollingLoop(CancellationToken stoppingToken) { - _logger.LogInformation("S7轮询线程已启动。"); + NlogHelper.Info("S7轮询线程已启动。"); // 注册取消回调,当服务停止时记录日志。 - stoppingToken.Register(() => _logger.LogInformation("S7后台服务正在停止。")); + stoppingToken.Register(() => NlogHelper.Info("S7后台服务正在停止。")); // 初始加载S7设备列表。 _s7Devices = _dataServices.Devices?.Where(d => d.ProtocolType == ProtocolType.S7 && d.IsActive) @@ -131,18 +132,18 @@ namespace PMSWPF.Services // _logger.LogDebug($"开始轮询变量,当前时间:{DateTime.Now}"); readCount = 0; TGCount = 0; - + Stopwatch stopwatch = Stopwatch.StartNew(); // 启动计时器,测量轮询耗时 PollS7Devices(stoppingToken); // 执行S7设备轮询 stopwatch.Stop(); // 停止计时器 // _logger.LogDebug($"结束轮询变量,当前时间:{DateTime.Now}"); - _logger.LogDebug($"读取变量数:{readCount}个,跳过变量数:{TGCount}总耗时:{stopwatch.ElapsedMilliseconds}ms"); - + NlogHelper.Info($"读取变量数:{readCount}个,跳过变量数:{TGCount}总耗时:{stopwatch.ElapsedMilliseconds}ms"); + // 短暂休眠以防止CPU占用过高,并控制轮询频率。 Thread.Sleep(1000); } - _logger.LogInformation("S7轮询线程已停止。"); + NlogHelper.Info("S7轮询线程已停止。"); } @@ -162,11 +163,11 @@ namespace PMSWPF.Services plcClient = new Plc(device.CpuType, device.Ip, (short)device.Prot, device.Rack, device.Slot); plcClient.Open(); // 尝试打开连接。 _s7PlcClients[device.Id] = plcClient; // 将新创建的客户端添加到字典。 - _logger.LogInformation($"已连接到S7 PLC: {device.Name} ({device.Ip})"); + NlogHelper.Info($"已连接到S7 PLC: {device.Name} ({device.Ip})"); } catch (Exception ex) { - _logger.LogError(ex, $"连接S7 PLC失败: {device.Name} ({device.Ip})"); + NotificationHelper.ShowError($"连接S7 PLC失败: {device.Name} ({device.Ip})", ex); return null; // 连接失败,返回null。 } } @@ -176,11 +177,11 @@ namespace PMSWPF.Services try { plcClient.Open(); // 尝试重新打开连接。 - _logger.LogInformation($"已重新连接到S7 PLC: {device.Name} ({device.Ip})"); + NlogHelper.Info($"已重新连接到S7 PLC: {device.Name} ({device.Ip})"); } catch (Exception ex) { - _logger.LogError(ex, $"重新连接S7 PLC失败: {device.Name} ({device.Ip})"); + NotificationHelper.ShowError($"重新连接S7 PLC失败: {device.Name} ({device.Ip})", ex); return null; // 重新连接失败,返回null。 } } @@ -197,7 +198,8 @@ namespace PMSWPF.Services // 如果没有活跃的S7设备,则等待一段时间后重试。 if (_s7Devices == null || !_s7Devices.Any()) { - _logger.LogDebug("未找到活跃的S7设备进行轮询。等待5秒后重试。"); + NlogHelper.Info( + "未找到活跃的S7设备进行轮询。等待5秒后重试。"); try { // 使用CancellationToken来使等待可取消。 @@ -247,7 +249,7 @@ namespace PMSWPF.Services if (!s7Variables.Any()) { - _logger.LogDebug($"设备 {device.Name} 没有找到活跃的S7变量。"); + NlogHelper.Info($"设备 {device.Name} 没有找到活跃的S7变量。"); return; } @@ -262,10 +264,10 @@ namespace PMSWPF.Services // 获取变量的轮询间隔。 if (!_pollingIntervals.TryGetValue(variable.PollLevelType, out var interval)) { - _logger.LogWarning($"未知轮询级别 {variable.PollLevelType},跳过变量 {variable.Name}。"); + NlogHelper.Info($"未知轮询级别 {variable.PollLevelType},跳过变量 {variable.Name}。"); continue; } - + // 检查是否达到轮询时间。 if ((DateTime.Now - variable.LastPollTime) < interval) { @@ -275,7 +277,6 @@ namespace PMSWPF.Services try { - // 从PLC读取变量值。 var value = plcClient.Read(variable.S7Address); if (value != null) @@ -290,19 +291,18 @@ namespace PMSWPF.Services readCount++; // _logger.LogDebug($"线程ID:{Environment.CurrentManagedThreadId},已读取变量 {variable.Name}: {variable.DataValue}"); } - } catch (Exception ex) { - _logger.LogError(ex, $"从设备 {device.Name} 读取变量 {variable.Name} 失败。"); + NotificationHelper.ShowError( $"从设备 {device.Name} 读取变量 {variable.Name} 失败。",ex); } // stopwatch.Stop(); - // _logger.LogInformation($"读取变量耗时:{stopwatch.ElapsedMilliseconds}ms "); + // NlogHelper.Info($"读取变量耗时:{stopwatch.ElapsedMilliseconds}ms "); } } catch (Exception ex) { - _logger.LogError(ex, $"设备 {device.Name} 批量读取过程中发生错误。"); + NotificationHelper.ShowError($"设备 {device.Name} 批量读取过程中发生错误。",ex); } } @@ -313,7 +313,7 @@ namespace PMSWPF.Services /// 表示异步操作的任务。 public override async Task StopAsync(CancellationToken stoppingToken) { - _logger.LogInformation("S7 Background Service is stopping."); + NlogHelper.Info("S7 Background Service is stopping."); // 发出信号,请求轮询线程停止。 _cancellationTokenSource?.Cancel(); @@ -326,7 +326,7 @@ namespace PMSWPF.Services if (plcClient.IsConnected) { plcClient.Close(); - _logger.LogInformation($"Closed S7 PLC connection: {plcClient.IP}"); + NlogHelper.Info($"Closed S7 PLC connection: {plcClient.IP}"); } } diff --git a/ViewModels/DevicesViewModel.cs b/ViewModels/DevicesViewModel.cs index 2d8e431..905ea40 100644 --- a/ViewModels/DevicesViewModel.cs +++ b/ViewModels/DevicesViewModel.cs @@ -19,7 +19,7 @@ public partial class DevicesViewModel : ViewModelBase private readonly DataServices _dataServices; private readonly IDialogService _dialogService; private readonly DeviceRepository _deviceRepository; - private readonly ILogger _logger; + private readonly MenuRepository _menuRepository; private readonly VarTableRepository _varTableRepository; @@ -42,13 +42,13 @@ public partial class DevicesViewModel : ViewModelBase /// 对话框服务。 /// 数据服务。 public DevicesViewModel( - ILogger logger, IDialogService dialogService, DataServices dataServices + IDialogService dialogService, DataServices dataServices ) { _deviceRepository = new DeviceRepository(); _varTableRepository = new VarTableRepository(); _menuRepository = new MenuRepository(); - _logger = logger; + _dialogService = dialogService; _dataServices = dataServices; @@ -69,7 +69,7 @@ public partial class DevicesViewModel : ViewModelBase // 如果用户取消或对话框未返回设备,则直接返回 if (device == null) { - _logger.LogInformation("用户取消了添加设备操作。"); + NlogHelper.Info("用户取消了添加设备操作。"); return; } diff --git a/ViewModels/VariableTableViewModel.cs b/ViewModels/VariableTableViewModel.cs index af5bc0b..5482980 100644 --- a/ViewModels/VariableTableViewModel.cs +++ b/ViewModels/VariableTableViewModel.cs @@ -3,20 +3,17 @@ using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using iNKORE.UI.WPF.Modern.Controls; using Newtonsoft.Json; -using NLog; using PMSWPF.Data.Repositories; using PMSWPF.Enums; using PMSWPF.Extensions; using PMSWPF.Helper; using PMSWPF.Models; using PMSWPF.Services; -using ILogger = Microsoft.Extensions.Logging.ILogger; namespace PMSWPF.ViewModels; partial class VariableTableViewModel : ViewModelBase { - private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private readonly IDialogService _dialogService; // private readonly ILogger _logger; @@ -174,7 +171,7 @@ partial class VariableTableViewModel : ViewModelBase processingDialog?.Hide(); string msgSuccess = $"成功导入变量:{resVarDataCount}个。"; - Logger.Info(msgSuccess); + NlogHelper.Info(msgSuccess); NotificationHelper.ShowSuccess(msgSuccess); } diff --git a/Views/Dialogs/DeviceDialog.xaml.cs b/Views/Dialogs/DeviceDialog.xaml.cs index 2343079..be88203 100644 --- a/Views/Dialogs/DeviceDialog.xaml.cs +++ b/Views/Dialogs/DeviceDialog.xaml.cs @@ -1,15 +1,13 @@ using System.Windows; using iNKORE.UI.WPF.Modern.Controls; +using PMSWPF.Helper; using PMSWPF.Models; using PMSWPF.ViewModels.Dialogs; -using NLog; // Add NLog using directive namespace PMSWPF.Views.Dialogs; public partial class DeviceDialog { - private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); // Add Logger - public DeviceDialog(DeviceDialogViewModel viewModel) { InitializeComponent(); @@ -18,11 +16,11 @@ public partial class DeviceDialog // Log the ProtocolType value if (viewModel.Device != null) { - Logger.Info($"DeviceDialog opened. Device ProtocolType: {viewModel.Device.ProtocolType}"); + NlogHelper.Info($"DeviceDialog opened. Device ProtocolType: {viewModel.Device.ProtocolType}"); } else { - Logger.Info("DeviceDialog opened. Device is null."); + NlogHelper.Info("DeviceDialog opened. Device is null."); } } diff --git a/Views/MainView.xaml.cs b/Views/MainView.xaml.cs index 98daba9..02b0850 100644 --- a/Views/MainView.xaml.cs +++ b/Views/MainView.xaml.cs @@ -1,7 +1,6 @@ using System.Windows; using iNKORE.UI.WPF.Modern.Controls; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; using PMSWPF.Enums; using PMSWPF.Helper; using PMSWPF.Models; @@ -16,17 +15,15 @@ namespace PMSWPF.Views; public partial class MainView : Window { private readonly DataServices _dataServices; - private readonly ILogger _logger; private MainViewModel _viewModel; - public MainView(DataServices dataServices, ILogger logger) + public MainView(DataServices dataServices) { InitializeComponent(); _viewModel = App.Current.Services.GetRequiredService(); _dataServices = dataServices; - _logger = logger; DataContext = _viewModel; - _logger.LogInformation("主界面加载成功"); + NlogHelper.Info("主界面加载成功"); } ///