From 6e91d188fe7848b58f90484c08f3953fb2232139 Mon Sep 17 00:00:00 2001 From: Boxfriend Date: Wed, 17 Dec 2025 00:15:23 -0500 Subject: [PATCH] ensure proper disposal of objects --- Program.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Program.cs b/Program.cs index bd24964..642d98c 100644 --- a/Program.cs +++ b/Program.cs @@ -10,14 +10,16 @@ using Serilog.Events; var resetEvent = new ManualResetEvent(false); using var tokenSource = new CancellationTokenSource(); -Console.CancelKeyPress += (sender, eventArgs) => +Console.CancelKeyPress += (_, eventArgs) => { - resetEvent?.Set(); - tokenSource?.Cancel(); + Log.Debug("Canceling with CancelKeyPress"); + Dispose(); eventArgs.Cancel = true; Log.CloseAndFlush(); }; +AppDomain.CurrentDomain.ProcessExit += (_, _) => Dispose(); + SetupLogging(); var config = LoadConfig(); @@ -30,6 +32,13 @@ resetEvent.WaitOne(); return; +void Dispose() +{ + resetEvent?.Set(); + tokenSource?.Cancel(); + Log.CloseAndFlush(); +} + static void SetupLogging() { var minLevel = (Environment.GetEnvironmentVariable("LOG_LEVEL") ?? "info").ToUpperInvariant() switch