-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
752 lines (656 loc) · 30.5 KB
/
Program.cs
File metadata and controls
752 lines (656 loc) · 30.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
using System;
using System.Windows.Forms;
using System.Threading;
using System.Globalization;
using System.IO;
using System.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using PricisApp.Interfaces;
using PricisApp.Repositories;
using PricisApp.Services;
using PricisApp.Models;
using FluentMigrator.Runner;
using Microsoft.Extensions.Logging;
using Serilog;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using MaterialSkin;
using MaterialSkin.Controls;
using PricisApp.ViewModels;
using Sentry;
using Microsoft.AppCenter;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;
using PricisApp.Core.Entities;
using PricisApp.UI;
using System.Runtime.Versioning;
namespace PricisApp;
internal static class Program
{
private static ServiceProvider? _serviceProvider;
private static Serilog.ILogger? _serilogLogger;
private static IConfiguration? _configuration;
private static AppSettings? _appSettings;
public static AppSettings AppSettings => _appSettings ?? throw new InvalidOperationException("Application settings not initialized");
public static IConfiguration Configuration => _configuration ?? throw new InvalidOperationException("Configuration not initialized");
[STAThread]
[System.Runtime.Versioning.SupportedOSPlatform("windows7.0")]
static void Main(string[] args)
{
// Add startup logging
StartupDebug.LogStartup("Application starting");
// Check if running in console mode
if (args.Length > 0 && args[0] == "--console")
{
StartupDebug.LogStartup("Running in console mode");
RunConsoleMode();
return;
}
// Check if running in diagnostic mode
if (args.Length > 0 && args[0] == "--diagnose")
{
StartupDebug.LogStartup("Running in diagnostic mode");
RunDiagnosticModeAsync().GetAwaiter().GetResult();
return;
}
try
{
StartupDebug.LogStartup("PricisApp starting normal mode");
Console.WriteLine("PricisApp starting...");
// Load configuration first
_configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile("appsettings.user.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables() // Use this instead of AddInMemoryCollection
.Build();
Console.WriteLine("Configuration loaded");
// Bind configuration to settings object
_appSettings = new AppSettings();
_configuration.Bind(_appSettings);
Console.WriteLine("Settings bound to object");
// Ensure Logs directory exists
try
{
Directory.CreateDirectory("Logs");
Console.WriteLine("Logs directory created");
}
catch (Exception ex)
{
Console.WriteLine($"Error creating Logs directory: {ex.Message}");
// Continue execution even if we can't create the logs directory
}
// Configure Serilog from configuration
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(_configuration)
.CreateLogger();
Console.WriteLine("Serilog configured");
// Initialize Sentry if DSN is configured
if (!string.IsNullOrEmpty(_appSettings.Sentry?.Dsn))
{
SentrySdk.Init(options =>
{
options.Dsn = _appSettings.Sentry.Dsn;
options.Debug = _appSettings.Sentry.Debug;
options.SendDefaultPii = _appSettings.Sentry.SendDefaultPii;
options.AttachStacktrace = _appSettings.Sentry.AttachStacktrace;
options.Environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";
// Add null check for MainModule to prevent null reference exceptions
var mainModule = Process.GetCurrentProcess().MainModule;
options.Release = mainModule != null
? $"pricisapp@{FileVersionInfo.GetVersionInfo(mainModule.FileName).ProductVersion}"
: "pricisapp@1.0.0";
});
Console.WriteLine("Sentry crash reporting initialized");
}
// Initialize AppCenter if SecretKey is configured
if (!string.IsNullOrEmpty(_appSettings.AppCenter?.SecretKey))
{
var services = new List<Type>();
if (_appSettings.AppCenter.EnableAnalytics)
services.Add(typeof(Analytics));
if (_appSettings.AppCenter.EnableCrashes)
services.Add(typeof(Crashes));
if (services.Count > 0)
{
AppCenter.Start(_appSettings.AppCenter.SecretKey, services.ToArray());
Console.WriteLine("AppCenter initialized with services");
}
}
// Set up global exception handlers
SetupExceptionHandling();
Console.WriteLine("Exception handling configured");
// Configure services
ConfigureServices();
Console.WriteLine("Services configured");
// Get config service to read theme
var configService = _serviceProvider!.GetRequiredService<IConfigurationService>();
var theme = configService.GetDefaultTheme();
Console.WriteLine($"Theme loaded: {theme}");
// Initialize MaterialSkinManager
var materialSkinManager = MaterialSkinManager.Instance;
materialSkinManager.Theme = theme.Equals("Dark", StringComparison.OrdinalIgnoreCase)
? MaterialSkinManager.Themes.DARK
: MaterialSkinManager.Themes.LIGHT;
materialSkinManager.ColorScheme = new ColorScheme(
Primary.BlueGrey800,
Primary.BlueGrey900,
Primary.BlueGrey500,
Accent.LightBlue200,
TextShade.WHITE
);
Console.WriteLine("MaterialSkinManager initialized");
// Set culture to invariant for consistent number formatting
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
// Fix database if needed
try
{
Console.WriteLine("Checking if database needs to be fixed...");
DatabaseFix.FixDatabaseAsync().GetAwaiter().GetResult();
Console.WriteLine("Database fix completed successfully");
}
catch (Exception ex)
{
Console.WriteLine($"ERROR during database fix: {ex.Message}");
Console.WriteLine(ex.StackTrace);
// Show error message to user
MessageBox.Show(
$"There was a problem with the database: {ex.Message}\n\nThe application will continue to start, but may not function correctly.",
"Database Error",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}
// Enable visual styles
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Set default font from configuration
try
{
if (_appSettings.UI?.DefaultFont != null)
{
Application.SetDefaultFont(new System.Drawing.Font(
_appSettings.UI.DefaultFont.Name,
_appSettings.UI.DefaultFont.Size));
Console.WriteLine("UI settings applied");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error setting default font: {ex.Message}");
// Continue without custom font
}
// Create main form with DI
try
{
var mainForm = _serviceProvider!.GetRequiredService<Form1>();
Console.WriteLine("Main form created");
// Run the application
Console.WriteLine("Starting application...");
Application.Run(mainForm);
}
catch (Exception ex)
{
StartupDebug.LogStartup($"ERROR: Error creating or running main form: {ex.Message}");
StartupDebug.LogStartup($"Stack trace: {ex.StackTrace}");
Console.WriteLine($"Error creating or running main form: {ex.Message}");
Console.WriteLine(ex.StackTrace);
try
{
MessageBox.Show(
$"Error starting application: {ex.Message}\n\n{ex.StackTrace}",
"Application Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
catch (Exception msgEx)
{
StartupDebug.LogStartup($"ERROR: Failed to show error message box: {msgEx.Message}");
}
}
}
catch (Exception ex)
{
StartupDebug.LogStartup($"FATAL ERROR: {ex.Message}");
StartupDebug.LogStartup($"Stack trace: {ex.StackTrace}");
Console.WriteLine($"FATAL ERROR: {ex.Message}");
Console.WriteLine(ex.StackTrace);
try
{
Log.Fatal(ex, "Application terminated unexpectedly");
}
catch (Exception logEx)
{
StartupDebug.LogStartup($"ERROR: Failed to log fatal error: {logEx.Message}");
}
try
{
ShowFatalErrorMessage(ex);
}
catch (Exception msgEx)
{
StartupDebug.LogStartup($"ERROR: Failed to show fatal error message: {msgEx.Message}");
}
}
finally
{
// Dispose of services when application exits
_serviceProvider?.Dispose();
// Ensure all Sentry events are sent
if (!string.IsNullOrEmpty(_appSettings?.Sentry?.Dsn))
{
SentrySdk.FlushAsync(TimeSpan.FromSeconds(2)).GetAwaiter().GetResult();
}
// Flush and close Serilog
Log.CloseAndFlush();
}
}
[System.Runtime.Versioning.SupportedOSPlatform("windows7.0")]
private static void SetupExceptionHandling()
{
// Handle exceptions in all threads
Application.ThreadException += (sender, e) =>
{
Log.Error(e.Exception, "Thread exception occurred");
HandleException(e.Exception, "Thread Exception");
};
// Handle exceptions in the AppDomain
AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
{
var exception = e.ExceptionObject as Exception;
Log.Fatal(exception, "Unhandled AppDomain exception occurred");
if (e.IsTerminating)
{
Log.Fatal("Application is terminating due to unhandled exception");
ShowFatalErrorMessage(exception);
}
else
{
HandleException(exception, "AppDomain Exception");
}
};
// Set the unhandled exception mode to catch all Windows Forms errors
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
}
[SupportedOSPlatform("windows")]
private static void HandleException(Exception? exception, string source)
{
try
{
var errorMessage = $"An error occurred in {source}:\n\n{exception?.Message}";
Log.Error(exception, errorMessage);
// Capture exception in Sentry if configured
if (!string.IsNullOrEmpty(_appSettings?.Sentry?.Dsn))
{
if (exception != null) { SentrySdk.CaptureException(exception); }
}
// Track exception in AppCenter if configured
if (!string.IsNullOrEmpty(_appSettings?.AppCenter?.SecretKey) &&
_appSettings?.AppCenter?.EnableCrashes == true)
{
Crashes.TrackError(exception);
}
// Show a user-friendly error message
MessageBox.Show(
errorMessage,
"Application Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
catch (Exception ex)
{
// If we can't handle the exception gracefully, at least log it
Log.Fatal(ex, "Error in exception handler");
}
}
[SupportedOSPlatform("windows")]
private static void ShowFatalErrorMessage(Exception? exception)
{
try
{
// Capture fatal exception in Sentry if configured
if (!string.IsNullOrEmpty(_appSettings?.Sentry?.Dsn))
{
if (exception != null) { SentrySdk.CaptureException(exception); }
// Ensure all events are sent before the application exits
SentrySdk.FlushAsync(TimeSpan.FromSeconds(2)).GetAwaiter().GetResult();
}
// Track fatal exception in AppCenter if configured
if (!string.IsNullOrEmpty(_appSettings?.AppCenter?.SecretKey) &&
_appSettings?.AppCenter?.EnableCrashes == true)
{
Crashes.TrackError(exception);
}
var message = "A fatal error has occurred and the application needs to close.\n\n" +
$"Error details: {exception?.Message}\n\n" +
"Please check the log files for more information.";
MessageBox.Show(
message,
"Fatal Application Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
catch
{
// Last resort if even showing a message box fails
}
}
[System.Runtime.Versioning.SupportedOSPlatform("windows7.0")]
private static void ConfigureServices()
{
Console.WriteLine("Starting ConfigureServices...");
var services = new ServiceCollection();
// Add configuration
services.AddSingleton(_configuration!);
services.AddSingleton(_appSettings!);
Console.WriteLine("Added configuration to services");
// Configure memory cache with custom options for better performance
services.AddMemoryCache(options =>
{
// options.SizeLimit = 1024; // Set a reasonable size limit for the cache (DISABLED to fix error)
options.ExpirationScanFrequency = TimeSpan.FromMinutes(5); // Scan for expired items every 5 minutes
options.CompactionPercentage = 0.2; // Remove 20% of entries when size limit is reached
});
Console.WriteLine("Configured memory cache with optimized settings");
// Register database helper as singleton
services.AddSingleton<DatabaseHelper>(provider => {
Console.WriteLine("Creating DatabaseHelper...");
var config = provider.GetRequiredService<IConfiguration>();
var appSettings = provider.GetRequiredService<AppSettings>();
var dbHelper = new DatabaseHelper(config, appSettings);
Console.WriteLine("DatabaseHelper created");
return dbHelper;
});
// Register repositories
services.AddScoped<ICategoryRepository>(provider => {
Console.WriteLine("Creating CategoryRepository...");
var dbHelper = provider.GetRequiredService<DatabaseHelper>();
return new CategoryRepository(dbHelper.Connection);
});
services.AddScoped<ITaskRepository>(provider => {
Console.WriteLine("Creating TaskRepository...");
var dbHelper = provider.GetRequiredService<DatabaseHelper>();
var categoryRepo = provider.GetRequiredService<ICategoryRepository>();
return new TaskRepository(dbHelper.Connection);
});
// Register services
services.AddScoped<ITaskService, TaskService>();
services.AddScoped<ISessionService, SessionService>();
services.AddSingleton<IConfigurationService, ConfigurationService>();
Console.WriteLine("Added repositories and services");
// Register main form
services.AddTransient<Form1>(provider =>
{
Console.WriteLine("Creating Form1...");
var dbHelper = provider.GetRequiredService<DatabaseHelper>();
var taskService = provider.GetRequiredService<ITaskService>();
var sessionService = provider.GetRequiredService<ISessionService>();
var categoryRepo = provider.GetRequiredService<ICategoryRepository>();
var configService = provider.GetRequiredService<IConfigurationService>();
var form = new Form1(dbHelper, taskService, sessionService, categoryRepo, configService, provider);
Console.WriteLine("Form1 created");
return form;
});
// Register Dashboard form and viewmodel
services.AddTransient<DashboardViewModel>();
services.AddTransient<UI.DashboardForm>(provider => {
Console.WriteLine("Creating DashboardForm...");
var viewModel = provider.GetRequiredService<DashboardViewModel>();
var dbHelper = provider.GetRequiredService<DatabaseHelper>();
return new UI.DashboardForm(viewModel, dbHelper);
});
Console.WriteLine("Added forms to services");
// Register Serilog as the logging provider
_serilogLogger = Log.Logger;
services.AddLogging(loggingBuilder =>
{
loggingBuilder.ClearProviders();
loggingBuilder.AddSerilog(_serilogLogger, dispose: false);
});
Console.WriteLine("Added logging to services");
// Register DatabaseHealthCheck
services.AddTransient<PricisApp.Infrastructure.HealthChecks.DatabaseHealthCheck>();
// Register health checks
services.AddHealthChecks()
.AddCheck<PricisApp.Infrastructure.HealthChecks.DatabaseHealthCheck>("database_health_check");
Console.WriteLine("Registered health checks");
// Register optimized TaskService with caching
services.AddSingleton<TaskService>(sp =>
{
Console.WriteLine("Creating TaskService singleton with optimized caching...");
var db = sp.GetRequiredService<DatabaseHelper>();
var cache = sp.GetRequiredService<IMemoryCache>();
var config = sp.GetRequiredService<IConfiguration>();
var logger = sp.GetRequiredService<ILogger<TaskService>>();
var taskService = new TaskService(db, cache, config, logger);
// Configure advanced caching options
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromMinutes(5))
.SetAbsoluteExpiration(TimeSpan.FromHours(1))
.SetPriority(CacheItemPriority.High)
.SetSize(1); // Relative size is 1 unit
// Add this options object to the cache so TaskService can use it
cache.Set("DefaultCacheOptions", cacheEntryOptions);
return taskService;
});
Console.WriteLine("Building service provider...");
_serviceProvider = services.BuildServiceProvider();
Console.WriteLine("Service provider built");
}
public static IServiceProvider CreateServices(string connectionString)
{
Console.WriteLine("Creating migration services with connection string...");
var services = new ServiceCollection();
services.AddFluentMigratorCore()
.ConfigureRunner(rb => rb
.AddSQLite()
.WithGlobalConnectionString(connectionString)
.ScanIn(typeof(Program).Assembly).For.Migrations())
.AddLogging(lb => lb.AddFluentMigratorConsole());
services.AddMemoryCache(options =>
{
// options.SizeLimit = 1024;
options.ExpirationScanFrequency = TimeSpan.FromMinutes(5);
options.CompactionPercentage = 0.2;
});
// Register configuration and logging for migration services
services.AddSingleton<IConfiguration>(_ => new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile("appsettings.user.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build());
services.AddSingleton<TaskService>(sp =>
{
Console.WriteLine("Creating TaskService for migrations...");
var db = sp.GetRequiredService<DatabaseHelper>();
var cache = sp.GetRequiredService<IMemoryCache>();
var config = sp.GetRequiredService<IConfiguration>();
var logger = sp.GetRequiredService<ILogger<TaskService>>();
return new TaskService(db, cache, config, logger);
});
Console.WriteLine("Building migration service provider...");
return services.BuildServiceProvider(false);
}
private static void RunConsoleMode()
{
Console.WriteLine("Running in console mode...");
try
{
// Load configuration
_configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile("appsettings.user.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables() // Use this instead of AddInMemoryCollection
.Build();
Console.WriteLine("Configuration loaded");
// Bind configuration to settings object
_appSettings = new AppSettings();
_configuration.Bind(_appSettings);
Console.WriteLine("Settings bound to object");
// Ensure Logs directory exists
try
{
Directory.CreateDirectory("Logs");
Console.WriteLine("Logs directory created");
}
catch (Exception ex)
{
Console.WriteLine($"Error creating Logs directory: {ex.Message}");
// Continue execution even if we can't create the logs directory
}
// Configure Serilog from configuration
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(_configuration)
.CreateLogger();
Console.WriteLine("Serilog configured");
// Configure services
var services = new ServiceCollection();
services.AddSingleton(_configuration);
services.AddSingleton(_appSettings);
// Add memory cache with optimized settings
services.AddMemoryCache(options =>
{
// options.SizeLimit = 1024;
options.ExpirationScanFrequency = TimeSpan.FromMinutes(5);
options.CompactionPercentage = 0.2;
});
// Register database helper as singleton
services.AddSingleton<DatabaseHelper>(provider => {
Console.WriteLine("Creating DatabaseHelper...");
var config = provider.GetRequiredService<IConfiguration>();
var appSettings = provider.GetRequiredService<AppSettings>();
var dbHelper = new DatabaseHelper(config, appSettings);
Console.WriteLine("DatabaseHelper created");
return dbHelper;
});
_serviceProvider = services.BuildServiceProvider();
Console.WriteLine("Services configured");
// Get the database helper
var dbHelper = _serviceProvider.GetRequiredService<DatabaseHelper>();
Console.WriteLine("Database helper retrieved");
// Test database connection
var connection = dbHelper.Connection;
Console.WriteLine($"Database connection state: {connection.State}");
Console.WriteLine("Console mode completed successfully");
}
catch (Exception ex)
{
Console.WriteLine($"FATAL ERROR: {ex.Message}");
Console.WriteLine(ex.StackTrace);
}
finally
{
_serviceProvider?.Dispose();
Log.CloseAndFlush();
}
}
private static async Task RunDiagnosticModeAsync()
{
Console.WriteLine("Running in diagnostic mode...");
try
{
// Check current directory
Console.WriteLine($"Current directory: {Directory.GetCurrentDirectory()}");
// Check if Logs directory exists
var logsDir = Path.Combine(Directory.GetCurrentDirectory(), "Logs");
Console.WriteLine($"Logs directory exists: {Directory.Exists(logsDir)}");
// Try to create Logs directory
try
{
Directory.CreateDirectory(logsDir);
Console.WriteLine("Logs directory created/confirmed");
}
catch (Exception ex)
{
Console.WriteLine($"Error creating Logs directory: {ex.Message}");
}
// Load configuration
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile("appsettings.user.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables() // Use this instead of AddInMemoryCollection
.Build();
Console.WriteLine("Configuration loaded");
// Check database settings
var dbFileName = configuration["Database:FileName"] ?? "TimeTracking.db";
Console.WriteLine($"Database filename from config: {dbFileName}");
// Check database file
var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var dbPath = Path.Combine(localAppData, dbFileName);
Console.WriteLine($"Database path: {dbPath}");
Console.WriteLine($"Database exists: {File.Exists(dbPath)}");
// Try to fix database
Console.WriteLine("Attempting to fix database...");
await DatabaseFix.FixDatabaseAsync();
// Check for running instances of the application
Console.WriteLine("\nChecking for running instances of the application...");
var currentProcess = Process.GetCurrentProcess();
var processes = Process.GetProcessesByName(currentProcess.ProcessName);
Console.WriteLine($"Found {processes.Length} instance(s) of {currentProcess.ProcessName}");
foreach (var process in processes)
{
if (process.Id != currentProcess.Id)
{
Console.WriteLine($" - Process ID: {process.Id}, Started: {process.StartTime}");
// Ask if user wants to terminate the process
Console.Write("Do you want to terminate this process? (y/n): ");
var response = Console.ReadLine()?.ToLower();
if (response == "y" || response == "yes")
{
try
{
process.Kill();
Console.WriteLine("Process terminated.");
}
catch (Exception ex)
{
Console.WriteLine($"Error terminating process: {ex.Message}");
}
}
}
}
// Run database optimization
Console.WriteLine("\nRunning database optimization...");
try
{
var services = new ServiceCollection();
services.AddSingleton(configuration);
var appSettings = new AppSettings();
configuration.Bind(appSettings);
services.AddSingleton(appSettings);
services.AddSingleton<DatabaseHelper>();
var serviceProvider = services.BuildServiceProvider();
var dbHelper = serviceProvider.GetRequiredService<DatabaseHelper>();
using var connection = dbHelper.Connection;
connection.Open();
using var cmd = connection.CreateCommand();
// Run SQLite VACUUM to reclaim space and defragment
cmd.CommandText = "VACUUM;";
await cmd.ExecuteNonQueryAsync();
Console.WriteLine("Database VACUUM completed successfully");
// Run ANALYZE to update statistics
cmd.CommandText = "ANALYZE;";
await cmd.ExecuteNonQueryAsync();
Console.WriteLine("Database ANALYZE completed successfully");
serviceProvider.Dispose();
}
catch (Exception ex)
{
Console.WriteLine($"Error optimizing database: {ex.Message}");
}
Console.WriteLine("\nDiagnostic completed. Press any key to exit.");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine($"Error in diagnostic mode: {ex.Message}");
Console.WriteLine(ex.StackTrace);
Console.WriteLine("\nPress any key to exit.");
Console.ReadKey();
}
}
}