| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- using ivf_tl_Services;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Threading;
- namespace ivf_tl_Operate
- {
- /// <summary>
- /// Interaction logic for App.xaml
- /// </summary>
- public partial class App : Application
- {
- private static Mutex instance;
- public App()
- {
- Log4netHelper.WriteLog("App构造函数运行");
- //首先注册开始和退出事件
- this.Startup += new StartupEventHandler(App_Startup);
- this.Exit += new ExitEventHandler(App_Exit);
- }
- protected override void OnStartup(StartupEventArgs e)
- {
- bool isNotRunning; //互斥体判断
- instance = new Mutex(true, "ivf_tl_Operate", out isNotRunning); //同步基元变量
- if (!isNotRunning) // 如果不是未运行状态
- {
- MessageBox.Show("程序已启动 ");
- App.Current.Shutdown();
- return;
- }
- base.OnStartup(e);
- AppContext.SetSwitch("Switch.System.Windows.Input.Stylus.EnablePointerSupport", true);
- }
- private void App_Exit(object sender, ExitEventArgs e)
- {
- }
- private void App_Startup(object sender, StartupEventArgs e)
- {
- //UI线程未捕获异常处理事件
- this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
- //Task线程内未捕获异常处理事件
- TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
- //非UI线程未捕获异常处理事件
- AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
- // M5-02-5:启动时一次性凭据迁移(明文 passWord/engineerPwd → 密文,幂等)。须先于读取凭据。
- ivf_tl_Operate.Helpers.AppConfigHelper.MigratePlaintextCredentials();
- // M8-P3b:操作日志组件启动初始化(一次)。project=operate,Kafka 从 App.config kfkaIP+kfkaPort。
- InitOperationLog();
- ChangeLanguage(ConfigurationManager.AppSettings["Language"].ToString());
- }
- /// <summary>
- /// M8-P3b:初始化操作日志组件。Kafka 地址取 App.config 的 kfkaIP/kfkaPort;topic=tl-oplog。
- /// 全 try 兜底:日志初始化失败绝不影响业务启动。
- /// </summary>
- private void InitOperationLog()
- {
- try
- {
- string kafkaIp = ConfigurationManager.AppSettings["kfkaIP"]?.ToString() ?? "127.0.0.1";
- string kafkaPort = ConfigurationManager.AppSettings["kfkaPort"]?.ToString() ?? "9092";
- Aivfo.OperationLog.OperationLogger.Init(o =>
- {
- o.Project = "operate";
- o.KafkaBootstrapServers = $"{kafkaIp}:{kafkaPort}";
- o.Topic = "tl-oplog";
- });
- Log4netHelper.WriteLog($"[M8-P3b]操作日志组件已初始化 kafka={kafkaIp}:{kafkaPort} topic=tl-oplog");
- }
- catch (Exception ex)
- {
- // 兜底:组件本身也兜底,这里再保一层,绝不因日志初始化影响启动。
- try { Log4netHelper.WriteLog($"[M8-P3b]操作日志组件初始化失败(已忽略):{ex.Message}"); } catch { }
- }
- }
- /// <summary>
- /// 非UI线程未捕获异常处理事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- /// <exception cref="NotImplementedException"></exception>
- private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
- {
- try
- {
- if (e.IsTerminating)
- {
- Log4netHelper.WriteLog($"非UI线程发生致命错误,程序即将终止");
- }
- if (e.ExceptionObject is Exception ex)
- {
- if (ex.InnerException != null)
- {
- Log4netHelper.WriteLog($"非UI线程异常详细:{ex.InnerException.Message}{ex.InnerException.StackTrace}");
- }
- Log4netHelper.WriteLog($"非UI线程异常:{ex.Message}{ex.StackTrace}");
- }
- else
- {
- Log4netHelper.WriteLog($"非UI线程异常:异常对象类型不是Exception");
- }
- }
- catch (Exception exx)
- {
- if (exx.InnerException != null)
- {
- Log4netHelper.WriteLog($"捕获非UI线程异常时发生异常详细:{exx.InnerException.Message}{exx.InnerException.StackTrace}");
- }
- Log4netHelper.WriteLog($"捕获非UI线程异常时发生异常:{exx.Message}{exx.StackTrace}");
- }
- }
- /// <summary>
- /// Task线程内未捕获异常处理事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- /// <exception cref="NotImplementedException"></exception>
- private void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
- {
- try
- {
- if (e.Exception.InnerException != null)
- {
- Log4netHelper.WriteLog($"Task线程异常详细:{e.Exception.InnerException.Message}{e.Exception.InnerException.StackTrace}");
- }
- Log4netHelper.WriteLog($"Task线程异常:{e.Exception.Message}{e.Exception.StackTrace}");
- e.SetObserved();//设置该异常已察觉(这样处理后就不会引起程序崩溃)
- }
- catch (Exception ex)
- {
- if (ex.InnerException != null)
- {
- Log4netHelper.WriteLog($"捕获Task线程异常时发生异常详细:{ex.InnerException.Message}{ex.InnerException.StackTrace}");
- }
- Log4netHelper.WriteLog($"捕获Task线程异常时发生异常:{ex.Message}{ex.StackTrace}");
- }
- finally
- {
- e.SetObserved();//设置该异常已察觉(这样处理后就不会引起程序崩溃)
- }
- }
- /// <summary>
- /// UI线程未捕获异常处理事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- /// <exception cref="NotImplementedException"></exception>
- private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
- {
-
- try
- {
- if (e.Exception.InnerException != null)
- {
- Log4netHelper.WriteLog($"UI线程异常详细:{e.Exception.InnerException.Message}{e.Exception.InnerException.StackTrace}");
- }
- Log4netHelper.WriteLog($"UI线程异常:{e.Exception.Message}{e.Exception.StackTrace}");
- //把 Handled 属性设为true,表示此异常已处理,程序可以继续运行,不会强制退出
- }
- catch (Exception ex)
- {
- //此时程序出现严重异常,将强制结束退出
- if (ex.InnerException != null)
- {
- Log4netHelper.WriteLog($"捕获UI线程异常时发生异常详细:{ex.InnerException.Message}{ex.InnerException.StackTrace}");
- }
- Log4netHelper.WriteLog($"捕获UI线程异常时发生异常:{ex.Message}{ex.StackTrace}");
- }
- finally
- {
- e.Handled = true;
- }
- }
- public void ChangeLanguage(string languageName)
- {
- try
- {
- ResourceDictionary langRd = null;
- #if DEBUG
- string xamlFilePath = @"C:\PersonalSpace\work\1 VisualWorkSpace\SurfaceLan\" + languageName;
- #else
- string xamlFilePath = $"{System.AppDomain.CurrentDomain.BaseDirectory}Resources\\Language\\{languageName}";
- #endif
- if (!File.Exists(xamlFilePath))
- {
- Log4netHelper.WriteLog($"切换语言失败,配置文件不存在:{xamlFilePath}");
- return;
- }
- using (var stream = new FileStream(xamlFilePath, FileMode.Open))
- {
- langRd = System.Windows.Markup.XamlReader.Load(stream) as ResourceDictionary;
- }
- if (langRd != null)
- {
- int count = Application.Current.Resources.MergedDictionaries.Count;
- if (count >= 1)
- {
- Application.Current.Resources.MergedDictionaries.RemoveAt(count - 1);
- }
- Application.Current.Resources.MergedDictionaries.Add(langRd);
- }
- else
- {
- Log4netHelper.WriteLog($"切换语言失败,文件转ResourceDictionary失败;{xamlFilePath}");
- }
- }
- catch (Exception ex)
- {
- Log4netHelper.WriteLog($"切换语言异常,{JsonConvert.SerializeObject(ex)}");
- return;
- }
- }
- }
- }
|