ltwork 1 год назад
Родитель
Сommit
99b6a49ade

+ 18 - 0
BlankApp1/BlankApp1/Events/LogEvent.cs

@@ -0,0 +1,18 @@
+using Prism.Events;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PLCTool.Events
+{
+    public  class LogEvent : PubSubEvent<LogMessage>
+    {
+    }
+    public class LogMessage
+    {
+        public DateTime LogTime { get; set; }
+        public string LogMsg { get; set; }
+    }
+}

+ 20 - 4
BlankApp1/BlankApp1/ViewModels/BasicConfigViewModel/DeviceViewModel.cs

@@ -29,12 +29,12 @@ namespace PLCTool.ViewModels.BasicConfigViewModel
         private readonly IBasicProjectService _iBasicProjectService;
         private readonly IMapper _mapper;
         private readonly IDialogService _dialog;
-        private readonly ILogger _logger;
+        private readonly Microsoft.Extensions.Logging.ILogger _logger;
         private readonly IEventAggregator _aggregator;
         private List<BasDeviceDto> allDeviceList = new List<BasDeviceDto>();//所有方案
         private List<BasDeviceDto> conditionDevices = new List<BasDeviceDto>();//符合条件的方案
         private List<CrumbViewModel> breadCrumbs = new List<CrumbViewModel>();
-        public DeviceViewModel(IBasicDeviceService iBasicDeviceService, IBasicDeviceKindService iBasicDeviceKindService, IBasicProjectService iBasicProjectService, IMapper mapper, IDialogService dialog, ILogger logger ,IEventAggregator aggregator)
+        public DeviceViewModel(IBasicDeviceService iBasicDeviceService, IBasicDeviceKindService iBasicDeviceKindService, IBasicProjectService iBasicProjectService, IMapper mapper, IDialogService dialog, Microsoft.Extensions.Logging.ILogger logger ,IEventAggregator aggregator)
         {
             _iBasicDeviceService = iBasicDeviceService;
             _iBasicDeviceKindService = iBasicDeviceKindService;
@@ -140,7 +140,12 @@ namespace PLCTool.ViewModels.BasicConfigViewModel
                         {
                             //重新读取PLC
                             GetPprojectConfig();
-                            _logger.LogInformation($"修改设备信息成功");
+                            _logger.LogInformation($"修改设备信息成功,设备名称为{deviceKindCon.device_name}");
+                            _aggregator.GetEvent<LogEvent>().Publish (new LogMessage
+                            {
+                                LogTime= DateTime.Now,
+                                LogMsg= $"修改设备信息成功,设备名称为{deviceKindCon.device_name}"
+                            });
                             MessageBox.Show("修改设备信息成功", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
                         }
                     }
@@ -158,6 +163,12 @@ namespace PLCTool.ViewModels.BasicConfigViewModel
                 var del = _iBasicDeviceService.Delete(id);
                 if (del)
                 {
+                    _logger.LogInformation($"删除设备成功");
+                    _aggregator.GetEvent<LogEvent>().Publish(new LogMessage
+                    {
+                        LogTime = DateTime.Now,
+                        LogMsg = $"删除设备成功"
+                    });
                     MessageBox.Show("删除成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
                     GetPprojectConfig();
                 }
@@ -264,7 +275,12 @@ namespace PLCTool.ViewModels.BasicConfigViewModel
                         {
                             //重新读取PLC
                             GetPprojectConfig();
-                            _logger.LogInformation($"添加设备成功");
+                            _logger.LogInformation($"添加设备成功,设备名为{returnValue.DeviceName}");
+                            _aggregator.GetEvent<LogEvent>().Publish(new LogMessage
+                            {
+                                LogTime = DateTime.Now,
+                                LogMsg = $"添加设备成功,设备名为{returnValue.DeviceName}"
+                            });
                             MessageBox.Show("添加设备成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
                         }
                     }

+ 46 - 1
BlankApp1/BlankApp1/ViewModels/MonitorManageViewModel/LogViewModel.cs

@@ -1,13 +1,16 @@
-using PLCTool.Events;
+using NLog;
+using PLCTool.Events;
 using PLCTool.Models;
 using Prism.Commands;
 using Prism.Events;
 using Prism.Mvvm;
 using System;
 using System.Collections.Generic;
+using System.Collections.ObjectModel;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using System.Windows;
 
 namespace PLCTool.ViewModels.MonitorManageViewModel
 {
@@ -19,7 +22,29 @@ namespace PLCTool.ViewModels.MonitorManageViewModel
         { 
             _aggregator = aggregator;
             OnLoadCommand = new DelegateCommand(OnLoad);
+            StartCommand = new DelegateCommand(Start);
+            StopCommand = new DelegateCommand(Stop);
+            CleanCommand = new DelegateCommand(Clean);
         }
+
+        private void Clean()
+        {
+            Application.Current.Dispatcher.Invoke(() =>
+            {
+                LogMessages.Clear();
+            });
+        }
+
+        private void Stop()
+        {
+            _aggregator.GetEvent<LogEvent>().Unsubscribe(OnLogEvent);
+        }
+
+        private void Start()
+        {
+            _aggregator.GetEvent<LogEvent>().Subscribe(OnLogEvent);
+        }
+
         private void OnLoad()
         {
             
@@ -28,7 +53,27 @@ namespace PLCTool.ViewModels.MonitorManageViewModel
             breadCrumbs.Add(new CrumbViewModel { Name = "监控管理" });
             breadCrumbs.Add(new CrumbViewModel { Name = "系统实时Log" });
             _aggregator.GetEvent<BreadEvent>().Publish(breadCrumbs);
+           
+        }
+
+        private void OnLogEvent(LogMessage logMessage)
+        {
+            Application.Current.Dispatcher.Invoke(() =>
+            {
+                LogMessages.Add($"{logMessage.LogTime}:  {logMessage.LogMsg}");
+            });
         }
         public DelegateCommand OnLoadCommand { set; get; }
+        public DelegateCommand StartCommand { set; get; }
+        public DelegateCommand StopCommand { set; get; }
+        public DelegateCommand CleanCommand { set; get; }
+        private ObservableCollection<string> logMessages=new ObservableCollection<string>();
+
+        public ObservableCollection<string> LogMessages
+        {
+            get { return logMessages; }
+            set { SetProperty(ref logMessages, value); }
+        }
+
     }
 }

+ 16 - 8
BlankApp1/BlankApp1/Views/MonitorManageView/LogView.xaml

@@ -20,18 +20,26 @@
             </b:EventTrigger>
         </b:Interaction.Triggers>
         <Grid.RowDefinitions>
-            <RowDefinition Height="60"/>
+            <RowDefinition Height="50"/>
+            <RowDefinition Height="40"/>
             <RowDefinition/>
         </Grid.RowDefinitions>
         <StackPanel  Grid.Row="0" Orientation="Horizontal">
             <Button  Content="开始监控"   Margin="5,0"  Command="{Binding StartCommand}" Style="{StaticResource NormalButtonStyle}" />
-            <Button  Content="停止监控"   Margin="5,0"  Command="{Binding QueryCommand}" Style="{StaticResource NormalButtonStyle}" />
-            <Button  Content="清理Log"   Margin="5,0"  Command="{Binding QueryCommand}" Style="{StaticResource NormalButtonStyle}" />
-        </StackPanel>
-        <StackPanel Grid.Row="1">
-            <nlog:NlogViewer x:Name="logCtrl" Background="Transparent"  TimeWidth="120" LoggerNameWidth="100" LevelWidth="80" MessageWidth="200" ExceptionWidth="120">
-             
-            </nlog:NlogViewer>
+            <Button  Content="停止监控"   Margin="5,0"  Command="{Binding StopCommand}" Style="{StaticResource NormalButtonStyle}" />
+            <Button  Content="清理Log"   Margin="5,0"  Command="{Binding CleanCommand}" Style="{StaticResource NormalButtonStyle}" />
         </StackPanel>
+        <TextBlock Grid.Row="1" Text="实时运行日志 :" Style="{StaticResource NormalTextBlockStyle}" FontSize="15" Margin="5,0"/>
+        <Grid Grid.Row="2">
+            <ListBox ItemsSource="{Binding LogMessages}">
+                <ListBox.ItemTemplate>
+                    <DataTemplate>
+                        <StackPanel>
+                            <TextBlock Text="{Binding}" />
+                        </StackPanel>
+                    </DataTemplate>
+                </ListBox.ItemTemplate>
+            </ListBox>
+        </Grid>
     </Grid>
 </UserControl>