App.xaml.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using ivf_tl_Services;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. using System.Data;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Threading;
  13. namespace ivf_tl_Operate
  14. {
  15. /// <summary>
  16. /// Interaction logic for App.xaml
  17. /// </summary>
  18. public partial class App : Application
  19. {
  20. private static Mutex instance;
  21. public App()
  22. {
  23. Log4netHelper.WriteLog("App构造函数运行");
  24. //首先注册开始和退出事件
  25. this.Startup += new StartupEventHandler(App_Startup);
  26. this.Exit += new ExitEventHandler(App_Exit);
  27. }
  28. protected override void OnStartup(StartupEventArgs e)
  29. {
  30. bool isNotRunning; //互斥体判断
  31. instance = new Mutex(true, "ivf_tl_Operate", out isNotRunning); //同步基元变量
  32. if (!isNotRunning) // 如果不是未运行状态
  33. {
  34. MessageBox.Show("程序已启动 ");
  35. App.Current.Shutdown();
  36. return;
  37. }
  38. base.OnStartup(e);
  39. AppContext.SetSwitch("Switch.System.Windows.Input.Stylus.EnablePointerSupport", true);
  40. }
  41. private void App_Exit(object sender, ExitEventArgs e)
  42. {
  43. }
  44. private void App_Startup(object sender, StartupEventArgs e)
  45. {
  46. //UI线程未捕获异常处理事件
  47. this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
  48. //Task线程内未捕获异常处理事件
  49. TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
  50. //非UI线程未捕获异常处理事件
  51. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  52. // M5-02-5:启动时一次性凭据迁移(明文 passWord/engineerPwd → 密文,幂等)。须先于读取凭据。
  53. ivf_tl_Operate.Helpers.AppConfigHelper.MigratePlaintextCredentials();
  54. // M8-P3b:操作日志组件启动初始化(一次)。project=operate,Kafka 从 App.config kfkaIP+kfkaPort。
  55. InitOperationLog();
  56. ChangeLanguage(ConfigurationManager.AppSettings["Language"].ToString());
  57. }
  58. /// <summary>
  59. /// M8-P3b:初始化操作日志组件。Kafka 地址取 App.config 的 kfkaIP/kfkaPort;topic=tl-oplog。
  60. /// 全 try 兜底:日志初始化失败绝不影响业务启动。
  61. /// </summary>
  62. private void InitOperationLog()
  63. {
  64. try
  65. {
  66. string kafkaIp = ConfigurationManager.AppSettings["kfkaIP"]?.ToString() ?? "127.0.0.1";
  67. string kafkaPort = ConfigurationManager.AppSettings["kfkaPort"]?.ToString() ?? "9092";
  68. Aivfo.OperationLog.OperationLogger.Init(o =>
  69. {
  70. o.Project = "operate";
  71. o.KafkaBootstrapServers = $"{kafkaIp}:{kafkaPort}";
  72. o.Topic = "tl-oplog";
  73. });
  74. Log4netHelper.WriteLog($"[M8-P3b]操作日志组件已初始化 kafka={kafkaIp}:{kafkaPort} topic=tl-oplog");
  75. }
  76. catch (Exception ex)
  77. {
  78. // 兜底:组件本身也兜底,这里再保一层,绝不因日志初始化影响启动。
  79. try { Log4netHelper.WriteLog($"[M8-P3b]操作日志组件初始化失败(已忽略):{ex.Message}"); } catch { }
  80. }
  81. }
  82. /// <summary>
  83. /// 非UI线程未捕获异常处理事件
  84. /// </summary>
  85. /// <param name="sender"></param>
  86. /// <param name="e"></param>
  87. /// <exception cref="NotImplementedException"></exception>
  88. private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  89. {
  90. try
  91. {
  92. if (e.IsTerminating)
  93. {
  94. Log4netHelper.WriteLog($"非UI线程发生致命错误,程序即将终止");
  95. }
  96. if (e.ExceptionObject is Exception ex)
  97. {
  98. if (ex.InnerException != null)
  99. {
  100. Log4netHelper.WriteLog($"非UI线程异常详细:{ex.InnerException.Message}{ex.InnerException.StackTrace}");
  101. }
  102. Log4netHelper.WriteLog($"非UI线程异常:{ex.Message}{ex.StackTrace}");
  103. }
  104. else
  105. {
  106. Log4netHelper.WriteLog($"非UI线程异常:异常对象类型不是Exception");
  107. }
  108. }
  109. catch (Exception exx)
  110. {
  111. if (exx.InnerException != null)
  112. {
  113. Log4netHelper.WriteLog($"捕获非UI线程异常时发生异常详细:{exx.InnerException.Message}{exx.InnerException.StackTrace}");
  114. }
  115. Log4netHelper.WriteLog($"捕获非UI线程异常时发生异常:{exx.Message}{exx.StackTrace}");
  116. }
  117. }
  118. /// <summary>
  119. /// Task线程内未捕获异常处理事件
  120. /// </summary>
  121. /// <param name="sender"></param>
  122. /// <param name="e"></param>
  123. /// <exception cref="NotImplementedException"></exception>
  124. private void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
  125. {
  126. try
  127. {
  128. if (e.Exception.InnerException != null)
  129. {
  130. Log4netHelper.WriteLog($"Task线程异常详细:{e.Exception.InnerException.Message}{e.Exception.InnerException.StackTrace}");
  131. }
  132. Log4netHelper.WriteLog($"Task线程异常:{e.Exception.Message}{e.Exception.StackTrace}");
  133. e.SetObserved();//设置该异常已察觉(这样处理后就不会引起程序崩溃)
  134. }
  135. catch (Exception ex)
  136. {
  137. if (ex.InnerException != null)
  138. {
  139. Log4netHelper.WriteLog($"捕获Task线程异常时发生异常详细:{ex.InnerException.Message}{ex.InnerException.StackTrace}");
  140. }
  141. Log4netHelper.WriteLog($"捕获Task线程异常时发生异常:{ex.Message}{ex.StackTrace}");
  142. }
  143. finally
  144. {
  145. e.SetObserved();//设置该异常已察觉(这样处理后就不会引起程序崩溃)
  146. }
  147. }
  148. /// <summary>
  149. /// UI线程未捕获异常处理事件
  150. /// </summary>
  151. /// <param name="sender"></param>
  152. /// <param name="e"></param>
  153. /// <exception cref="NotImplementedException"></exception>
  154. private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
  155. {
  156. try
  157. {
  158. if (e.Exception.InnerException != null)
  159. {
  160. Log4netHelper.WriteLog($"UI线程异常详细:{e.Exception.InnerException.Message}{e.Exception.InnerException.StackTrace}");
  161. }
  162. Log4netHelper.WriteLog($"UI线程异常:{e.Exception.Message}{e.Exception.StackTrace}");
  163. //把 Handled 属性设为true,表示此异常已处理,程序可以继续运行,不会强制退出
  164. }
  165. catch (Exception ex)
  166. {
  167. //此时程序出现严重异常,将强制结束退出
  168. if (ex.InnerException != null)
  169. {
  170. Log4netHelper.WriteLog($"捕获UI线程异常时发生异常详细:{ex.InnerException.Message}{ex.InnerException.StackTrace}");
  171. }
  172. Log4netHelper.WriteLog($"捕获UI线程异常时发生异常:{ex.Message}{ex.StackTrace}");
  173. }
  174. finally
  175. {
  176. e.Handled = true;
  177. }
  178. }
  179. public void ChangeLanguage(string languageName)
  180. {
  181. try
  182. {
  183. ResourceDictionary langRd = null;
  184. #if DEBUG
  185. string xamlFilePath = @"C:\PersonalSpace\work\1 VisualWorkSpace\SurfaceLan\" + languageName;
  186. #else
  187. string xamlFilePath = $"{System.AppDomain.CurrentDomain.BaseDirectory}Resources\\Language\\{languageName}";
  188. #endif
  189. if (!File.Exists(xamlFilePath))
  190. {
  191. Log4netHelper.WriteLog($"切换语言失败,配置文件不存在:{xamlFilePath}");
  192. return;
  193. }
  194. using (var stream = new FileStream(xamlFilePath, FileMode.Open))
  195. {
  196. langRd = System.Windows.Markup.XamlReader.Load(stream) as ResourceDictionary;
  197. }
  198. if (langRd != null)
  199. {
  200. int count = Application.Current.Resources.MergedDictionaries.Count;
  201. if (count >= 1)
  202. {
  203. Application.Current.Resources.MergedDictionaries.RemoveAt(count - 1);
  204. }
  205. Application.Current.Resources.MergedDictionaries.Add(langRd);
  206. }
  207. else
  208. {
  209. Log4netHelper.WriteLog($"切换语言失败,文件转ResourceDictionary失败;{xamlFilePath}");
  210. }
  211. }
  212. catch (Exception ex)
  213. {
  214. Log4netHelper.WriteLog($"切换语言异常,{JsonConvert.SerializeObject(ex)}");
  215. return;
  216. }
  217. }
  218. }
  219. }