using LogoForceTestApp.Modules.MainModule.Models; using LogoForceTestApp.Modules.MainModule; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Configuration; using Prism.DryIoc; using Prism.Ioc; using Prism.Modularity; using Serilog.Events; using Serilog; using System; using System.IO; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; using LogoForceTestApp.Services.Interfaces; using LogoForceTestApp.Services; using LogoForceTestApp.Views; using DryIoc; using Team.Utility; using DryIoc.Microsoft.DependencyInjection; using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder; using Repository; using Microsoft.EntityFrameworkCore; using AutoMapper.EquivalencyExpression; using System.Linq; using System.Reflection; using TouchSocket.Core; using TouchSocket.Sockets; using System.Net.Sockets; using TcpClient = TouchSocket.Sockets.TcpClient; using HandyControl.Controls; using Window = System.Windows.Window; namespace LogoForceTestApp { /// /// App.xaml 的交互逻辑 /// public partial class App { System.Threading.Mutex mutex; public App() { this.Startup += new StartupEventHandler(App_Startup); } void App_Startup(object sender, StartupEventArgs e) { bool ret; mutex = new System.Threading.Mutex(true, "ElectronicNeedleTherapySystem", out ret); if (!ret) { //System.Windows.MessageBox.Show("已有一个程序实例运行"); Environment.Exit(0); } } private static readonly string logPath = AppDomain.CurrentDomain.BaseDirectory + "logs.db"; private readonly ILogger _log = new LoggerConfiguration() .WriteTo.Console(LogEventLevel.Debug) .WriteTo.Debug(LogEventLevel.Debug) .WriteTo.SQLite(logPath) .WriteTo.File(AppDomain.CurrentDomain.BaseDirectory + "logs/log.log", rollingInterval: RollingInterval.Day) .CreateLogger(); public static string Version { get; private set; } protected override void OnStartup(StartupEventArgs e) { var version = Assembly.GetExecutingAssembly().GetName().Version; Version = version.ToString(); var teamContext = new TeamDataContext(); teamContext.Database.Migrate(); DispatcherUnhandledException += App_DispatcherUnhandledException; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException; base.OnStartup(e); } protected override Window CreateShell() { return Container.Resolve(); } protected override void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.RegisterInstance(_log); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); TcpClient tcpClient = new TcpClient(); //载入配置 tcpClient.Setup(new TouchSocketConfig() .SetRemoteIPHost("192.168.10.200:7790") .ConfigureContainer(a => { a.AddConsoleLogger();//添加一个日志注入 })); try { tcpClient.Connect();//调用连接,当连接不成功时,会抛出异常。 Growl.Info("客户端成功连接"); } catch (Exception) { Growl.Warning("连接异常"); } containerRegistry.RegisterInstance(tcpClient); } protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog) { moduleCatalog.AddModule(); } protected override IContainerExtension CreateContainerExtension() { var services = new ServiceCollection(); Serilog.Log.Logger = _log; services.AddLogging(c => c.AddSerilog()); var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", false, true); var configuration = builder.Build(); services.ConfigureWritable(configuration.GetSection(nameof(AppSettings))); services.AddHttpClient(); services.AddAutoMapper(cfg => { cfg.AddCollectionMappers(); }); return new DryIocContainerExtension(new DryIoc.Container(CreateContainerRules()) .WithDependencyInjectionAdapter(services)); } private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) { _log.Information($"App TaskSchedulerUnobservedTaskException Exit:{e.Exception}"); } private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { _log.Information($"App CurrentDomainUnhandledException Exit:{e.ExceptionObject}"); } private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) { _log.Information($"App AppDispatcherUnhandledException Exit:{e.Exception}"); } } }