App.xaml.cs 10 KB

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