将Logger都更改为使用NLogHelper
This commit is contained in:
@@ -18,34 +18,47 @@ namespace PMSWPF.Services;
|
||||
/// </summary>
|
||||
public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
|
||||
{
|
||||
// 日志记录器,用于记录数据服务中的操作和错误。
|
||||
private readonly ILogger<DataServices> _logger;
|
||||
// 设备列表,使用ObservableProperty特性,当值改变时会自动触发属性变更通知。
|
||||
[ObservableProperty] private List<Device> _devices;
|
||||
[ObservableProperty]
|
||||
private List<Device> _devices;
|
||||
|
||||
// 变量表列表。
|
||||
[ObservableProperty] private List<VariableTable> _variableTables;
|
||||
[ObservableProperty]
|
||||
private List<VariableTable> _variableTables;
|
||||
|
||||
// 变量数据列表。
|
||||
[ObservableProperty] private List<VariableData> _variableDatas;
|
||||
[ObservableProperty]
|
||||
private List<VariableData> _variableDatas;
|
||||
|
||||
// 菜单树列表。
|
||||
[ObservableProperty] private List<MenuBean> menuTrees;
|
||||
[ObservableProperty]
|
||||
private List<MenuBean> menuTrees;
|
||||
|
||||
// MQTT配置列表。
|
||||
[ObservableProperty] private List<Mqtt> _mqtts;
|
||||
[ObservableProperty]
|
||||
private List<Mqtt> _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<List<Device>> OnDeviceListChanged;
|
||||
|
||||
// 菜单树列表变更事件,当菜单树数据更新时触发。
|
||||
public event EventHandler<List<MenuBean>> OnMenuTreeListChanged;
|
||||
|
||||
// MQTT列表变更事件,当MQTT配置数据更新时触发。
|
||||
public event EventHandler<List<Mqtt>> OnMqttListChanged;
|
||||
|
||||
// 变量数据变更事件,当变量数据更新时触发。
|
||||
public event EventHandler<List<VariableData>> OnVariableDataChanged;
|
||||
|
||||
@@ -86,9 +99,8 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
|
||||
/// 注入ILogger<DataServices>,并初始化各个数据仓库。
|
||||
/// </summary>
|
||||
/// <param name="logger">日志记录器实例。</param>
|
||||
public DataServices(ILogger<DataServices> logger)
|
||||
public DataServices()
|
||||
{
|
||||
_logger = logger;
|
||||
IsActive = true; // 激活消息接收器
|
||||
_deviceRepository = new DeviceRepository();
|
||||
_menuRepository = new MenuRepository();
|
||||
|
||||
@@ -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
|
||||
/// </summary>
|
||||
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
|
||||
/// <returns>表示异步操作的任务。</returns>
|
||||
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
|
||||
/// <returns>表示异步操作的任务。</returns>
|
||||
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
|
||||
/// <param name="mqtts">更新后的MQTT配置列表。</param>
|
||||
private async void HandleMqttListChanged(object sender, List<Mqtt> 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
|
||||
/// <param name="variableDatas">更新后的变量数据列表。</param>
|
||||
private async void HandleVariableDataChanged(object sender, List<VariableData> variableDatas)
|
||||
{
|
||||
Logger.Info("Variable data changed. Reloading variable associations."); // 记录变量数据变化信息
|
||||
NlogHelper.Info("Variable data changed. Reloading variable associations."); // 记录变量数据变化信息
|
||||
// 重新加载变量数据。
|
||||
await LoadVariableData();
|
||||
}
|
||||
|
||||
@@ -11,14 +11,11 @@ namespace PMSWPF.Services;
|
||||
|
||||
public partial class NavgatorServices : ObservableRecipient, IRecipient<NavgatorMessage>
|
||||
{
|
||||
private readonly ILogger<NavgatorServices> _logger;
|
||||
|
||||
// [ObservableProperty]
|
||||
private ViewModelBase currentViewModel;
|
||||
|
||||
public NavgatorServices(ILogger<NavgatorServices> logger)
|
||||
public NavgatorServices()
|
||||
{
|
||||
_logger = logger;
|
||||
IsActive = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,19 +18,21 @@ namespace PMSWPF.Services
|
||||
/// </summary>
|
||||
public class S7BackgroundService : BackgroundService
|
||||
{
|
||||
// 日志记录器,用于记录服务运行时的信息、警告和错误。
|
||||
private readonly ILogger<S7BackgroundService> _logger;
|
||||
// 数据服务实例,用于访问和操作应用程序数据,如设备配置。
|
||||
private readonly DataServices _dataServices;
|
||||
|
||||
// 存储S7 PLC客户端实例的字典,键为设备ID,值为Plc对象。
|
||||
private readonly Dictionary<int, Plc> _s7PlcClients = new Dictionary<int, Plc>();
|
||||
|
||||
// 轮询数据的线程。
|
||||
private Thread _pollingThread;
|
||||
|
||||
// 用于取消轮询操作的CancellationTokenSource。
|
||||
private CancellationTokenSource _cancellationTokenSource;
|
||||
|
||||
// 读取变量计数器。
|
||||
private int readCount = 0;
|
||||
|
||||
// 跳过变量计数器(未到轮询时间)。
|
||||
private int TGCount = 0;
|
||||
|
||||
@@ -65,9 +67,8 @@ namespace PMSWPF.Services
|
||||
/// </summary>
|
||||
/// <param name="logger">日志记录器实例。</param>
|
||||
/// <param name="dataServices">数据服务实例。</param>
|
||||
public S7BackgroundService(ILogger<S7BackgroundService> 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客户端可能需要重新初始化。");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -96,7 +97,7 @@ namespace PMSWPF.Services
|
||||
/// <returns>表示异步操作的任务。</returns>
|
||||
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
_logger.LogInformation("S7后台服务正在启动。");
|
||||
NlogHelper.Info("S7后台服务正在启动。");
|
||||
// 创建一个CancellationTokenSource,用于控制轮询线程的取消。
|
||||
_cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
|
||||
|
||||
@@ -116,9 +117,9 @@ namespace PMSWPF.Services
|
||||
/// <param name="stoppingToken">用于取消轮询的CancellationToken。</param>
|
||||
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
|
||||
/// <returns>表示异步操作的任务。</returns>
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user