| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- 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;
- // 当前生效的语言资源字典(运行时由 ChangeLanguage 维护)。用于按引用精确移除旧语言字典,
- // 避免历史 bug:盲删最后一个 MergedDictionary 误删 AdaptiveStyles(TouchMinSize)。
- private ResourceDictionary _currentLangRd;
- 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();
- // M8 点击层:注册全局按钮点击拦截,把每次点击记成 module="界面点击" 的操作日志(导航/点击轨迹)。
- // 开发阶段全量记录便于排障;上线用 §10 配置关模块「界面点击」即可。
- Helpers.ClickTrailLogger.Install();
- 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";
- // M8 §10 配置热生效:改本程序 exe 同目录的 oplog-config.json 即可按模块开关日志(≤15s 生效,免重启)。
- // 跟项目走、随 exe 部署(源文件在项目根 oplog-config.json);文件不存在=全开(开发默认)。
- o.ConfigFilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "oplog-config.json");
- });
- 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>
- // === 日志洪流抑制(防 8GB 复发)===
- // WPF 布局类异常被下面 e.Handled=true 吞掉后,WPF 会逐帧重新测量→同一异常每帧重抛。
- // 无节流时同一条异常每秒上千次、每条带完整堆栈(~4.7KB),曾致单日日志涨到 8GB。
- // 此处按异常签名节流:同签名 10s 内只放行一条,下次放行时补记期间被抑制的条数。
- private static readonly object _logThrottleLock = new object();
- private static readonly Dictionary<string, long[]> _logThrottle = new Dictionary<string, long[]>(); // signature -> [lastTicks, suppressedCount]
- private static readonly long _logThrottleWindowTicks = TimeSpan.FromSeconds(10).Ticks;
- /// <summary>
- /// 节流判定:同 <paramref name="signature"/> 在窗口期(10s)内只放行一次。
- /// 返回 true=应记录(<paramref name="suppressNote"/> 含上一窗口被抑制的条数提示);false=抑制本条。线程安全。
- /// </summary>
- private static bool ShouldLogThrottled(string signature, out string suppressNote)
- {
- suppressNote = string.Empty;
- long now = DateTime.Now.Ticks;
- lock (_logThrottleLock)
- {
- if (_logThrottle.TryGetValue(signature, out long[] st))
- {
- if (now - st[0] < _logThrottleWindowTicks)
- {
- st[1]++; // 仍在窗口内:累计被抑制条数,不记录
- return false;
- }
- if (st[1] > 0) suppressNote = $"(前 10s 抑制 {st[1]} 条相同异常)";
- st[0] = now;
- st[1] = 0;
- return true;
- }
- _logThrottle[signature] = new long[] { now, 0 };
- return true;
- }
- }
- private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
- {
- try
- {
- // 按异常消息节流(布局类异常被吞后会逐帧重抛,见上方说明)。
- if (ShouldLogThrottled("UI:" + (e.Exception?.Message ?? "null"), out string note))
- {
- if (e.Exception.InnerException != null)
- {
- Log4netHelper.WriteLog($"UI线程异常详细:{e.Exception.InnerException.Message}{e.Exception.InnerException.StackTrace}");
- }
- Log4netHelper.WriteLog($"UI线程异常{note}:{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)
- {
- var mds = Application.Current.Resources.MergedDictionaries;
- // 【8GB 洪流根因修复】原逻辑 RemoveAt(Count-1) 盲删"最后一个"合并字典。
- // 但 App.xaml 里最后一个是 AdaptiveStyles.xaml(全局 TouchMinSize 所在,M4-01-4 加在语言字典之后),
- // 故启动时本方法实删的是 AdaptiveStyles 而非旧语言字典 → 全局 TouchMinSize 丢失 →
- // SoftKeyboard 按键隐式样式 MinHeight={StaticResource TouchMinSize} 解析成 UnsetValue → 每帧抛 → 单日 8GB。
- // 改为:按引用移除"上一次语言字典";首次按 Source 含 /Language/ 匹配移除 App.xaml 预置语言字典,绝不碰 AdaptiveStyles。
- if (_currentLangRd != null)
- {
- mds.Remove(_currentLangRd);
- }
- else
- {
- for (int i = mds.Count - 1; i >= 0; i--)
- {
- string src = mds[i].Source?.OriginalString ?? string.Empty;
- if (src.IndexOf("/Language/", StringComparison.OrdinalIgnoreCase) >= 0)
- {
- mds.RemoveAt(i);
- break;
- }
- }
- }
- mds.Add(langRd);
- _currentLangRd = langRd;
- }
- else
- {
- Log4netHelper.WriteLog($"切换语言失败,文件转ResourceDictionary失败;{xamlFilePath}");
- }
- }
- catch (Exception ex)
- {
- Log4netHelper.WriteLog($"切换语言异常,{JsonConvert.SerializeObject(ex)}");
- return;
- }
- }
- }
- }
|