ensure proper disposal of objects
This commit is contained in:
15
Program.cs
15
Program.cs
@@ -10,14 +10,16 @@ using Serilog.Events;
|
|||||||
var resetEvent = new ManualResetEvent(false);
|
var resetEvent = new ManualResetEvent(false);
|
||||||
using var tokenSource = new CancellationTokenSource();
|
using var tokenSource = new CancellationTokenSource();
|
||||||
|
|
||||||
Console.CancelKeyPress += (sender, eventArgs) =>
|
Console.CancelKeyPress += (_, eventArgs) =>
|
||||||
{
|
{
|
||||||
resetEvent?.Set();
|
Log.Debug("Canceling with CancelKeyPress");
|
||||||
tokenSource?.Cancel();
|
Dispose();
|
||||||
eventArgs.Cancel = true;
|
eventArgs.Cancel = true;
|
||||||
Log.CloseAndFlush();
|
Log.CloseAndFlush();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
AppDomain.CurrentDomain.ProcessExit += (_, _) => Dispose();
|
||||||
|
|
||||||
SetupLogging();
|
SetupLogging();
|
||||||
|
|
||||||
var config = LoadConfig();
|
var config = LoadConfig();
|
||||||
@@ -30,6 +32,13 @@ resetEvent.WaitOne();
|
|||||||
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
void Dispose()
|
||||||
|
{
|
||||||
|
resetEvent?.Set();
|
||||||
|
tokenSource?.Cancel();
|
||||||
|
Log.CloseAndFlush();
|
||||||
|
}
|
||||||
|
|
||||||
static void SetupLogging()
|
static void SetupLogging()
|
||||||
{
|
{
|
||||||
var minLevel = (Environment.GetEnvironmentVariable("LOG_LEVEL") ?? "info").ToUpperInvariant() switch
|
var minLevel = (Environment.GetEnvironmentVariable("LOG_LEVEL") ?? "info").ToUpperInvariant() switch
|
||||||
|
|||||||
Reference in New Issue
Block a user