using Quartz; using Quartz.Impl; using Quartz.Simpl; using Serilog; using Serilog.Events; using System; using System.Configuration; using System.Data; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using System.Windows; using WPFWCSDemo.Schedules.CircleRGV; using WPFWCSDemo.Schedules.CommonTask; using WPFWCSDemo.Schedules.TaskFinish; using WPFWCSDemo.Utilities; using WPFWCSDemo.ViewModels; namespace WPFWCSDemo { /// /// Interaction logic for App.xaml /// public partial class App : Application { private IScheduler _scheduler; public App() { InitializeComponent(); //Serilog其他 //Serilog日志配置 //Debug- //Info-调度信息 //Warning-设备读写/连接异常 //Error-执行异常,交互异常 Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() //.Filter.ByIncludingOnly(logEvent => logEvent.Level == LogEventLevel.Debug) .WriteTo.File(path: $"D:/Logs/Debug/Debug.txt", outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level}] {Message}{NewLine}{Exception}", restrictedToMinimumLevel: LogEventLevel.Debug, rollingInterval: RollingInterval.Day) .WriteTo.File(path: $"D:/Logs/Info/Info.txt", outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level}] {Message}{NewLine}{Exception}", restrictedToMinimumLevel: LogEventLevel.Information, rollingInterval: RollingInterval.Day) .WriteTo.File(path: $"D:/Logs/Warning/Warning.txt", outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level}] {Message}{NewLine}{Exception}", restrictedToMinimumLevel: LogEventLevel.Warning, rollingInterval: RollingInterval.Day) .WriteTo.File(path: $"D:/Logs/Error/Error.txt", outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level}] {Message}{NewLine}{Exception}", restrictedToMinimumLevel: LogEventLevel.Error, rollingInterval: RollingInterval.Day) //.Filter.ByExcluding(logEvent => logEvent.Level == LogEventLevel.Warning) .Enrich.FromLogContext() .CreateLogger(); //获取app.config中sql连接字符串 SqlHelper.ConStr = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString; //检查进程是否已经启动,已经启动则显示报错信息退出程序。 string strProcessName = System.Diagnostics.Process.GetCurrentProcess().ProcessName; if (System.Diagnostics.Process.GetProcessesByName(strProcessName).Length > 1) { MessageBox.Show("对不起,本地已经有系统正在运行", "退出", MessageBoxButton.OK, MessageBoxImage.Exclamation); Current.Shutdown(); return; } } protected async override void OnStartup(StartupEventArgs e) { try { Log.Information("**********************************Application Is Started**********************************"); Log.Information("**********************************Application Is Started**********************************"); Log.Information("**********************************Application Is Started**********************************"); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); //============================================================================================== Log.Information("【Application is Starting】环穿设备初始化中..."); CircleRGVsViewModel.InitializeRgv(); //CircleRGVsViewModel.RGVStationInitialize(); Log.Information("【Application is Starting】环穿设备初始化完成!"); //============================================================================================== Log.Information("【Application is Starting】报警信息初始化中..."); AlarmViewModel.AlarmInitialize(); Log.Information("【Application is Starting】报警信息初始化完成!"); //============================================================================================== Log.Information("【Application is Starting】调度程序初始化中..."); ISchedulerFactory schedulerFactory = new StdSchedulerFactory(); _scheduler = schedulerFactory.GetScheduler().Result; await _scheduler.Start(); Log.Information("【Application is Starting】调度程序启动中..."); #region 注册环穿调度任务 Log.Information("【Application is Starting】设备调度初始化中..."); GetScheduleJob(typeof(CircleRGVSchedule), "circleRGVJob", "circleRGVTrigger", "circleRGVSchedule", 2000); #endregion //------------------------------------------------------------------------------- #region 调度任务处理 //任务处理---注册任务 Log.Information("【Application is Starting】任务处理调度初始化中..."); GetScheduleJob(typeof(TaskFinishSchedule), "taskHandlerJob", "taskHandlerTrigger", "taskHandlerSchedule", 1000); #endregion //------------------------------------------------------------------------------- #region 全局任务 //全局任务---注册任务 Log.Information("【Application is Starting】全局任务初始化中..."); GetScheduleJob(typeof(CommonTask), "globalTaskJob", "globalTTrigger", "commonTask", 1000); #endregion Log.Information("【Application is Starting】调度程序初始化完成!"); //============================================================================================== stopwatch.Stop(); Log.Information($"【Application is Starting】WCS工程已启动,用时{stopwatch.Elapsed.TotalSeconds}s"); SqlHelper.NewEventLog("WCS调度已启动", "SYS"); } catch (Exception ex) { Log.Error($"【Application is Starting】{ex.Message}"); Log.Error($"【Application is Starting】{ex.InnerException}"); Current.Shutdown(); } } protected override void OnExit(ExitEventArgs e) { base.OnExit(e); Log.Information("【Application is Exiting】程序正在退出!"); _scheduler.Shutdown(true).Wait(); //Current.Shutdown(); } /// /// Quartz调度任务注册 /// /// 类名 /// 作业名称 /// 触发器名称 /// 任务组名称 private async void GetScheduleJob(Type className, string jobName, string triggerName, string group,int time) { var method = typeof(JobBuilder) .GetMethods() .First(m => m.Name == "Create" && m.IsGenericMethod); var genericMethod = method.MakeGenericMethod(className); var jobBuilder = genericMethod.Invoke(null, null); //任务处理---注册任务 IJobDetail job = ((JobBuilder)jobBuilder) //.Create() .WithIdentity(jobName, group) .DisallowConcurrentExecution() .Build(); //任务处理---触发器 ITrigger trigger = TriggerBuilder .Create() .WithIdentity(triggerName, group) .StartNow() .WithSimpleSchedule(x => x .WithInterval(TimeSpan.FromMilliseconds(time)) .RepeatForever()) .Build(); await _scheduler.ScheduleJob(job, trigger); } } }