diff --git a/DentistryHandpieces/App.xaml.cs b/DentistryHandpieces/App.xaml.cs index be08ffb..2e617d3 100644 --- a/DentistryHandpieces/App.xaml.cs +++ b/DentistryHandpieces/App.xaml.cs @@ -1,14 +1,96 @@ -using System.Configuration; -using System.Data; +using System.IO; using System.Windows; +using System.Windows.Threading; +using Serilog; +using Serilog.Events; -namespace DentistryHandpieces +namespace DentistryHandpieces; + +public partial class App : Application { - /// - /// Interaction logic for App.xaml - /// - public partial class App : Application + protected override void OnStartup(StartupEventArgs e) { + ConfigureLogging(); + DispatcherUnhandledException += OnDispatcherUnhandledException; + AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; + TaskScheduler.UnobservedTaskException += OnUnobservedTaskException; + + Log.Information( + "应用启动,版本 {Version},系统 {OperatingSystem}", + typeof(App).Assembly.GetName().Version?.ToString() ?? "unknown", + Environment.OSVersion.VersionString); + + base.OnStartup(e); } + protected override void OnExit(ExitEventArgs e) + { + Log.Information("应用退出,退出代码 {ExitCode}", e.ApplicationExitCode); + Log.CloseAndFlush(); + base.OnExit(e); + } + + private static void ConfigureLogging() + { + string logDirectory = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), + "DentistryHandpieces", + "Logs"); + + try + { + CreateLogger(logDirectory); + Log.Information("日志系统初始化完成,目录 {LogDirectory}", logDirectory); + } + catch (Exception ex) + { + string fallbackDirectory = Path.Combine(Path.GetTempPath(), "DentistryHandpieces", "Logs"); + try + { + CreateLogger(fallbackDirectory); + Log.Warning(ex, "主日志目录初始化失败,已切换到临时目录 {LogDirectory}", fallbackDirectory); + } + catch (Exception fallbackException) + { + Log.Logger = new LoggerConfiguration().CreateLogger(); + System.Diagnostics.Debug.WriteLine($"Serilog initialization failed: {ex}; fallback failed: {fallbackException}"); + } + } + } + + private static void CreateLogger(string logDirectory) + { + Directory.CreateDirectory(logDirectory); + Log.Logger = new LoggerConfiguration() + .MinimumLevel.Information() + .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) + .Enrich.FromLogContext() + .Enrich.WithProperty("Application", "DentistryHandpieces") + .WriteTo.File( + Path.Combine(logDirectory, "app-.log"), + rollingInterval: RollingInterval.Day, + retainedFileCountLimit: 30, + fileSizeLimitBytes: 20 * 1024 * 1024, + rollOnFileSizeLimit: true, + shared: true, + flushToDiskInterval: TimeSpan.FromSeconds(1), + outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}") + .CreateLogger(); + } + + private static void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) + { + Log.Fatal(e.Exception, "UI线程发生未处理异常"); + } + + private static void OnUnhandledException(object? sender, UnhandledExceptionEventArgs e) + { + Log.Fatal(e.ExceptionObject as Exception, "应用域发生未处理异常,IsTerminating={IsTerminating}", e.IsTerminating); + } + + private static void OnUnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e) + { + Log.Error(e.Exception, "后台任务发生未观察异常"); + e.SetObserved(); + } } diff --git a/DentistryHandpieces/DentistryHandpieces.csproj b/DentistryHandpieces/DentistryHandpieces.csproj index a3df4df..5302ae0 100644 --- a/DentistryHandpieces/DentistryHandpieces.csproj +++ b/DentistryHandpieces/DentistryHandpieces.csproj @@ -11,8 +11,9 @@ - + + diff --git a/DentistryHandpieces/FileDialogService.cs b/DentistryHandpieces/FileDialogService.cs index 25b7945..56319be 100644 --- a/DentistryHandpieces/FileDialogService.cs +++ b/DentistryHandpieces/FileDialogService.cs @@ -13,8 +13,8 @@ public sealed class WpfFileDialogService : IFileDialogService { var dialog = new SaveFileDialog { - Title = "导出 Excel", - FileName = $"牙科手机验收记录_{createdAt:yyyyMMddHHmmss}.xlsx", + Title = "导出报表", + FileName = $"牙科手机测试报表_{createdAt:yyyyMMddHHmmss}.xlsx", Filter = "Excel 工作簿 (*.xlsx)|*.xlsx", DefaultExt = ".xlsx", AddExtension = true, diff --git a/DentistryHandpieces/MainWindow.xaml b/DentistryHandpieces/MainWindow.xaml index 2664a14..f44a4c2 100644 --- a/DentistryHandpieces/MainWindow.xaml +++ b/DentistryHandpieces/MainWindow.xaml @@ -301,10 +301,60 @@ + + + + + + + + + + + + + + + + + +