更新20260527

This commit is contained in:
GukSang.Jin
2026-05-27 12:53:06 +08:00
parent 1d087c00e8
commit 57ce28a1ea
10 changed files with 446 additions and 22 deletions

View File

@@ -1,6 +1,7 @@
using System.Configuration;
using System.Data;
using System.IO;
using System.Windows;
using System.Windows.Threading;
using Serilog;
namespace ConeCalorimeter
{
@@ -9,6 +10,74 @@ namespace ConeCalorimeter
/// </summary>
public partial class App : Application
{
}
protected override void OnStartup(StartupEventArgs e)
{
ConfigureLogging();
DispatcherUnhandledException += OnDispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
Log.Information("ConeCalorimeter application starting.");
base.OnStartup(e);
}
protected override void OnExit(ExitEventArgs e)
{
try
{
Log.Information("ConeCalorimeter application exiting with code {ExitCode}.", e.ApplicationExitCode);
}
finally
{
Log.CloseAndFlush();
}
base.OnExit(e);
}
private static void ConfigureLogging()
{
var logDirectory = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"ConeCalorimeter",
"Logs");
Directory.CreateDirectory(logDirectory);
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.WithProperty("Application", "ConeCalorimeter")
.WriteTo.File(
Path.Combine(logDirectory, "app-.log"),
rollingInterval: RollingInterval.Day,
retainedFileCountLimit: 30,
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, "Unhandled WPF dispatcher exception.");
}
private static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is Exception exception)
{
Log.Fatal(exception, "Unhandled AppDomain exception. IsTerminating={IsTerminating}", e.IsTerminating);
return;
}
Log.Fatal(
"Unhandled AppDomain exception object: {ExceptionObject}. IsTerminating={IsTerminating}",
e.ExceptionObject,
e.IsTerminating);
}
private static void OnUnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
{
Log.Error(e.Exception, "Unobserved task exception.");
}
}
}