diff --git a/App.xaml.cs b/App.xaml.cs index 9d47706..55e9ea4 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -3,6 +3,11 @@ using System.Data; using System.Windows; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using NLog; +using NLog.Extensions.Logging; +using PMSWPF.Data; +using PMSWPF.Data.Entities; using PMSWPF.Data.Repositories; using PMSWPF.Services; using PMSWPF.ViewModels; @@ -21,59 +26,58 @@ namespace PMSWPF public App() { var container = new ServiceCollection(); + + var nlog = LogManager.Setup().LoadConfigurationFromFile("Config/nlog.config").GetCurrentClassLogger(); + + container.AddLogging(loggingBuilder => + { + loggingBuilder.ClearProviders(); + loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); + loggingBuilder.AddNLog(); + }); + + container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); - container.AddSingleton(); + container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); - container.AddSingleton(dp => new MainView() - { DataContext = dp.GetRequiredService() }); + container.AddSingleton(); + container.AddSingleton(); + container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); Services = container.BuildServiceProvider(); + // 启动服务 + Services.GetRequiredService(); } protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); - CheckDb(); - + InitDB(); + MainWindow = Services.GetRequiredService(); MainWindow.Show(); } - private void CheckDb() + private void InitDB() { - + var _db = DbContext.GetInstance(); + _db.DbMaintenance.CreateDatabase(); + _db.CodeFirst.InitTables(); + _db.CodeFirst.InitTables(); + _db.CodeFirst.InitTables(); + _db.CodeFirst.InitTables(); + _db.CodeFirst.InitTables(); + _db.CodeFirst.InitTables(); + _db.CodeFirst.InitTables(); + _db.CodeFirst.InitTables(); } - - // [STAThread] - // static void Main(string[] args) - // { - // using IHost host = CreateHostBuilder(args).Build(); - // host.Start(); - // App app = new App(); - // app.InitializeComponent(); - // app.MainWindow = host.Services.GetRequiredService(); - // app.MainWindow.Visibility = Visibility.Visible; - // app.Run(); - // - // } - // - // private static IHostBuilder CreateHostBuilder(string[] args) - // { - // return Host.CreateDefaultBuilder(args).ConfigureServices(services => - // { - // - // services.AddHostedService(); - // services.AddSingleton(); - // services.AddSingleton(); - // }); - // } } } \ No newline at end of file diff --git a/Config/nlog.config b/Config/nlog.config new file mode 100644 index 0000000..ea10872 --- /dev/null +++ b/Config/nlog.config @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + INSERT INTO nlog ( + LogTime, Level, ThreadID,ThreadName,Callsite,CallsiteLineNumber,Message, + Logger, Exception + ) VALUES ( + @LogTime, @Level,@ThreadID,@ThreadName,@Callsite,@CallsiteLineNumber,@Message, + @Logger, @Exception + ) + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Data/Entities/DbMqtt.cs b/Data/Entities/DbMqtt.cs index 962422c..02a5308 100644 --- a/Data/Entities/DbMqtt.cs +++ b/Data/Entities/DbMqtt.cs @@ -2,7 +2,7 @@ namespace PMSWPF.Data.Entities; -[SugarTable("Mqtt")] + public class DbMqtt { [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//数据库是自增才配自增 diff --git a/Data/Entities/DbNlog.cs b/Data/Entities/DbNlog.cs new file mode 100644 index 0000000..2a9874c --- /dev/null +++ b/Data/Entities/DbNlog.cs @@ -0,0 +1,32 @@ +using SqlSugar; + +namespace PMSWPF.Data.Entities; + +[SugarTable("nlog")] +public class DbNlog +{ + // INSERT INTO [dbo].[NLog] ( + // [Application], [Logged], [Level], [ThreadID],[Message], + // [Logger], [Callsite], [Exception], [Url], [Action], [User] + // ) VALUES ( + // @Application, @Logged, @Level,@ThreadID, @Message, + // @Logger, @Callsite, @Exception, @Url, @Action, @User + // ) + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//数据库是自增才配自增 + public int Id { get; set; } + public DateTime LogTime { get; set; } + public string Level { get; set; } + public int ThreadID { get; set; } + [SugarColumn(IsNullable = true)] + public string ThreadName { get; set; } + public string Logger { get; set; } + public string Callsite { get; set; } + public int CallsiteLineNumber { get; set; } + public string Message { get; set; } + [SugarColumn(IsNullable = true,ColumnDataType = "text")] + public string Exception { get; set; } + + + + +} \ No newline at end of file diff --git a/Data/Repositories/BaseRepositories.cs b/Data/Repositories/BaseRepositories.cs index e44ac60..9bf24b7 100644 --- a/Data/Repositories/BaseRepositories.cs +++ b/Data/Repositories/BaseRepositories.cs @@ -9,13 +9,23 @@ public class BaseRepositories public BaseRepositories() { - _db = DbContext.GetInstance(); - _db.DbMaintenance.CreateDatabase(); - CheckDbTables(); + try + { + _db = DbContext.GetInstance(); + // _db.DbMaintenance.CreateDatabase(); + // CheckDbTables(); + } + catch (Exception e) + { + Console.WriteLine(e); + throw; + } } private void CheckDbTables() { + if(!_db.DbMaintenance.IsAnyTable()) + _db.CodeFirst.InitTables(); if(!_db.DbMaintenance.IsAnyTable()) _db.CodeFirst.InitTables(); if(!_db.DbMaintenance.IsAnyTable()) diff --git a/Helper/NotificationHelper.cs b/Helper/NotificationHelper.cs new file mode 100644 index 0000000..c52821f --- /dev/null +++ b/Helper/NotificationHelper.cs @@ -0,0 +1,14 @@ +using CommunityToolkit.Mvvm.Messaging; +using PMSWPF.Enums; +using PMSWPF.Message; + +namespace PMSWPF.Helper; + +public class NotificationHelper +{ + public static void ShowMessage(string msg, NotificationType notificationType=NotificationType.Info,bool isGlobal = false) + { + WeakReferenceMessenger.Default.Send( + new NotificationMessage(msg, notificationType)); + } +} \ No newline at end of file diff --git a/Message/NotificationMessage.cs b/Message/NotificationMessage.cs new file mode 100644 index 0000000..cc02d3f --- /dev/null +++ b/Message/NotificationMessage.cs @@ -0,0 +1,15 @@ +using CommunityToolkit.Mvvm.Messaging.Messages; +using PMSWPF.Enums; + +namespace PMSWPF.Message; + +public class NotificationMessage:ValueChangedMessage +{ + public NotificationType Type { get; set; } + public bool IsGlobal { get; set; } + public NotificationMessage(string msg,NotificationType type=NotificationType.Info,bool isGlobal=false) : base(msg) + { + this.Type = type; + this.IsGlobal = isGlobal; + } +} \ No newline at end of file diff --git a/PMSWPF.csproj b/PMSWPF.csproj index e208e8a..c17faff 100644 --- a/PMSWPF.csproj +++ b/PMSWPF.csproj @@ -14,8 +14,16 @@ + + + + + + Always + + diff --git a/Services/GrowlNotificationService.cs b/Services/GrowlNotificationService.cs index d5e046b..bba300c 100644 --- a/Services/GrowlNotificationService.cs +++ b/Services/GrowlNotificationService.cs @@ -1,11 +1,19 @@ -using HandyControl.Controls; +using System.Windows.Interop; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Messaging; +using HandyControl.Controls; using PMSWPF.Enums; +using PMSWPF.Message; using Notification = PMSWPF.Models.Notification; namespace PMSWPF.Services; -public class GrowlNotificationService : INotificationService +public class GrowlNotificationService :ObservableRecipient,IRecipient { + public GrowlNotificationService() + { + IsActive = true; + } public void Show(Notification notification) { if (notification == null ) @@ -74,4 +82,9 @@ public class GrowlNotificationService : INotificationService { Show(new Notification(){Message = message,Type = type,IsGlobal = IsGlobal}); } + + public void Receive(NotificationMessage message) + { + Show(message.Value,message.Type,message.IsGlobal); + } } \ No newline at end of file diff --git a/Services/LogService.cs b/Services/LogService.cs new file mode 100644 index 0000000..85fd185 --- /dev/null +++ b/Services/LogService.cs @@ -0,0 +1,6 @@ +namespace PMSWPF.Services; + +public class LogService +{ + +} \ No newline at end of file diff --git a/ViewModels/DevicesViewModel.cs b/ViewModels/DevicesViewModel.cs index edffe00..8f72aad 100644 --- a/ViewModels/DevicesViewModel.cs +++ b/ViewModels/DevicesViewModel.cs @@ -1,14 +1,17 @@ using System.Collections.ObjectModel; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; +using CommunityToolkit.Mvvm.Messaging; using HandyControl.Controls; using HandyControl.Data; +using Microsoft.Extensions.Logging; using PMSWPF.Data.Entities; using PMSWPF.Data.Repositories; using PMSWPF.Enums; using PMSWPF.Excptions; using PMSWPF.Extensions; using PMSWPF.Helper; +using PMSWPF.Message; using PMSWPF.Models; using PMSWPF.Services; using PMSWPF.ViewModels.Dialogs; @@ -22,15 +25,16 @@ public partial class DevicesViewModel : ViewModelBase { private readonly IDeviceDialogService _deviceDialogService; private readonly DevicesRepositories _devicesRepositories; - private readonly INotificationService _notificationService; - [ObservableProperty] private ObservableCollection _devices = new(); + private readonly ILogger _logger; + [ObservableProperty] + private ObservableCollection _devices = new(); - public DevicesViewModel(IDeviceDialogService deviceDialogService, DevicesRepositories devicesRepositories, - INotificationService notificationService) + public DevicesViewModel(IDeviceDialogService deviceDialogService, DevicesRepositories devicesRepositories,ILogger logger + ) { _deviceDialogService = deviceDialogService; _devicesRepositories = devicesRepositories; - _notificationService = notificationService; + _logger = logger; } public async Task OnLoadedAsync() @@ -71,22 +75,29 @@ public partial class DevicesViewModel : ViewModelBase { // MessageBox.Show("Device added successfully"); await OnLoadedAsync(); - _notificationService.Show($"设备添加成功:{device.Name}", NotificationType.Success); + var msg = $"设备添加成功:{device.Name}"; + _logger.LogInformation(msg); + NotificationHelper.ShowMessage(msg, NotificationType.Success); } } } catch (DbExistException e) { - _notificationService.Show($"设备添加失败:名称为{device?.Name}的设备已经存在。请更换是被名称", NotificationType.Error); + var msg = $"设备添加失败:名称为{device?.Name}的设备已经存在。请更换是被名称"; + _logger.LogError(msg); + NotificationHelper.ShowMessage(msg, NotificationType.Error); } catch (Exception e) { - _notificationService.Show($"添加设备的过程中发生错误:{e.Message}", NotificationType.Error); + var msg = $"添加设备的过程中发生错误:{e.Message}"; + _logger.LogError(msg); + NotificationHelper.ShowMessage(msg, NotificationType.Success); } } - public override void OnLoaded() + public override async void OnLoaded() { - OnLoadedAsync().Await((e) => { _deviceDialogService.ShowMessageDialog("", e.Message); }, () => { }); + // OnLoadedAsync().Await((e) => { _deviceDialogService.ShowMessageDialog("", e.Message); }, () => { }); + await OnLoadedAsync(); } } \ No newline at end of file diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs index 538664f..95232cf 100644 --- a/ViewModels/MainViewModel.cs +++ b/ViewModels/MainViewModel.cs @@ -7,7 +7,7 @@ using PMSWPF.Services; namespace PMSWPF.ViewModels { - partial class MainViewModel : ObservableRecipient, IRecipient + public partial class MainViewModel : ObservableRecipient, IRecipient { private readonly NavgatorServices _navgatorServices; diff --git a/ViewModels/SettingViewModel.cs b/ViewModels/SettingViewModel.cs new file mode 100644 index 0000000..5c3c1d6 --- /dev/null +++ b/ViewModels/SettingViewModel.cs @@ -0,0 +1,9 @@ +namespace PMSWPF.ViewModels; + +public class SettingViewModel:ViewModelBase +{ + public override void OnLoaded() + { + + } +} \ No newline at end of file diff --git a/Views/MainView.xaml b/Views/MainView.xaml index d22ce7e..f71fea6 100644 --- a/Views/MainView.xaml +++ b/Views/MainView.xaml @@ -53,6 +53,16 @@ + + + + + + + + + + @@ -65,25 +75,7 @@ - - - - - - - - - - - - - - @@ -97,6 +89,9 @@ + + + diff --git a/Views/MainView.xaml.cs b/Views/MainView.xaml.cs index 9bf4185..7f89f02 100644 --- a/Views/MainView.xaml.cs +++ b/Views/MainView.xaml.cs @@ -1,6 +1,9 @@ using System.Windows; using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Messaging; using iNKORE.UI.WPF.Modern.Controls; +using Microsoft.Extensions.Logging; +using PMSWPF.Message; using PMSWPF.ViewModels; namespace PMSWPF.Views @@ -10,14 +13,24 @@ namespace PMSWPF.Views /// public partial class MainView : Window { + private readonly ILogger _logger; - public MainView() + public MainView(MainViewModel viewModel, ILogger logger) { - - InitializeComponent(); + _logger = logger; + InitializeComponent(); + this.DataContext=viewModel; + _logger.LogInformation("主界面加载成功"); + + } + /// + /// 左边菜单项被点击的事件,切换右边的视图 + /// + /// + /// private void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args) { NavigationViewItem? item = args.SelectedItem as NavigationViewItem; @@ -26,15 +39,24 @@ namespace PMSWPF.Views { case "Home": mainViewModel.NavgateTo(); + _logger.LogInformation("导航到到主页面"); break; case "Devices": + mainViewModel.NavgateTo(); + _logger.LogInformation("导航到到设备页面"); break; case "DataTransform": mainViewModel.NavgateTo(); + _logger.LogInformation("导航到到数据转换页面"); + break; + case "Setting": + mainViewModel.NavgateTo(); + _logger.LogInformation("导航到到设备页面"); break; default: mainViewModel.NavgateTo(); + _logger.LogInformation("没有设置Tag,默认导航到主页面。"); break; } diff --git a/Views/SettingView.xaml b/Views/SettingView.xaml new file mode 100644 index 0000000..f9d1218 --- /dev/null +++ b/Views/SettingView.xaml @@ -0,0 +1,12 @@ + + + + + diff --git a/Views/SettingView.xaml.cs b/Views/SettingView.xaml.cs new file mode 100644 index 0000000..a458ec9 --- /dev/null +++ b/Views/SettingView.xaml.cs @@ -0,0 +1,11 @@ +using System.Windows.Controls; + +namespace PMSWPF.Views; + +public partial class SettingView : UserControl +{ + public SettingView() + { + InitializeComponent(); + } +} \ No newline at end of file