public Form1() { InitializeComponent(); Log.Logger = new LoggerConfiguration() .WriteTo.Console() .WriteTo.File($"{AppDomain.CurrentDomain.BaseDirectory}\\log\\log-.txt", rollingInterval: RollingInterval.Day, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}") .CreateLogger(); //宣告Serilog初始化 GatewaySetting = InitialMethod.GatewayLoad(); APISetting = InitialMethod.APILoad(); if (GatewaySetting != null) { foreach (var item in GatewaySetting.Gateways) { GatewayTypeEnum = (GatewayTypeEnum)item.GatewayTypeEnum; switch (GatewayTypeEnum) { case GatewayTypeEnum.ModbusRTU: { SerialportComponent component = new SerialportComponent() { GateWaySetting = GatewaySetting, APISetting = APISetting }; component.MyWorkState = true; Field4Components.Add(component); } break; case GatewayTypeEnum.ModbusTCP: { TcpComponent component = new TcpComponent() { GateWaySetting = GatewaySetting, APISetting = APISetting }; component.MyWorkState = true; Field4Components.Add(component); } break; } } ApiComponent = new ApiComponent(); ApiComponent.APISetting = APISetting; ApiComponent.MyWorkState = true; timer1.Interval = 1000; timer1.Enabled = true; } }