77using Microsoft . Data . Sqlite ;
88using Microsoft . Extensions . Configuration ;
99using Microsoft . Extensions . Logging ;
10+ using Serilog ;
1011using SnapX . Core . CLI ;
1112using SnapX . Core . Hotkey ;
1213using SnapX . Core . Job ;
@@ -166,7 +167,7 @@ private static string PersonalPathConfigFilePath
166167
167168 private static string CustomConfigPath { get ; set ; }
168169 public static SqliteConnection DbConnection { get ; set ; }
169- public static string ShortenPath ( string path ) =>
170+ public static string ShortenPath ( string ? path ) =>
170171 OperatingSystem . IsWindows ( ) ? path : path . Replace ( Environment . GetEnvironmentVariable ( "HOME" ) ?? "" , "~" ) ;
171172
172173 public static string PersonalFolder =>
@@ -226,15 +227,15 @@ public static string ScreenshotsParentFolder
226227 }
227228 }
228229
229- public static readonly string DBPath = Path . Combine ( PersonalFolder , "SnapX.db" ) ;
230+ public static readonly string ? DBPath = Path . Combine ( PersonalFolder , "SnapX.db" ) ;
230231
231- public static string ImageEffectsFolder => Path . Combine ( PersonalFolder , "ImageEffects" ) ;
232+ public static string ? ImageEffectsFolder => Path . Combine ( PersonalFolder , "ImageEffects" ) ;
232233
233234 private static string PersonalPathDetectionMethod ;
234235
235236 public const string HistoryFileNameOld = "History.json" ;
236237
237- public static string HistoryFilePathOld
238+ public static string ? HistoryFilePathOld
238239 {
239240 get
240241 {
@@ -263,7 +264,7 @@ public void shutdown()
263264 CloseSequence ( ) ;
264265 }
265266 public Assembly [ ] GetAssemblies ( ) => AppDomain . CurrentDomain . GetAssemblies ( ) ;
266- public void start ( string [ ] args )
267+ public void start ( string ? [ ] args )
267268 {
268269 HandleExceptions ( ) ;
269270
@@ -291,6 +292,7 @@ public void start(string[] args)
291292 public long getStartupTime ( ) => StartTimer . ElapsedMilliseconds ;
292293 public EventAggregator getEventAggregator ( ) => EventAggregator ;
293294 public bool isSilent ( ) => SilentRun ;
295+ public static object ? aptabaseClientObject ;
294296
295297 // Supports the failed standard https://consoledonottrack.com/
296298 public static bool TelemetryEnabled ( ) => ! FeatureFlags . DisableTelemetry && ! Settings . DisableTelemetry &&
@@ -425,8 +427,9 @@ private static void Run()
425427 SQLitePCL . raw . SetProvider ( new SQLite3Provider_sqlite3 ( ) ) ;
426428 }
427429 SQLitePCL . Batteries_V2 . Init ( ) ;
430+ var dataSource = SnapX . Sandbox ? ":memory:" : DBPath ;
428431
429- var connectionString = new SqliteConnectionStringBuilder { DataSource = DBPath , Mode = SqliteOpenMode . ReadWriteCreate , Cache = SqliteCacheMode . Shared , ForeignKeys = true , Pooling = true , } . ToString ( ) ;
432+ var connectionString = new SqliteConnectionStringBuilder { DataSource = dataSource , Mode = SqliteOpenMode . ReadWriteCreate , Cache = SqliteCacheMode . Shared , ForeignKeys = true , Pooling = true , } . ToString ( ) ;
430433 DbConnection = new SqliteConnection ( connectionString ) ;
431434 RunWithTimeout ( ( ) => DbConnection . OpenAsync ( ) , $ "Opening the database connection at { DBPath } ") ;
432435 RunWithTimeout ( ( ) => DbConnection . ExecuteAsync ( "PRAGMA journal_mode=WAL;" ) , "Setting journal mode" ) ;
@@ -465,9 +468,14 @@ private static void Run()
465468 SettingManager . LoadSettings ( ) ;
466469 if ( TelemetryEnabled ( ) )
467470 {
468- var logger = DebugHelper . Logger . ForContext < AptabaseClient > ( ) as ILogger < AptabaseClient > ;
471+ var loggerFactory = LoggerFactory . Create ( builder =>
472+ {
473+ builder . ClearProviders ( ) ;
474+ builder . AddSerilog ( ) ;
475+ } ) ;
476+ var logger = loggerFactory . CreateLogger < AptabaseClient > ( ) ;
469477
470- var aptabaseClient = new AptabaseClient ( "A-US-4105716286" , new AptabaseOptions ( )
478+ var aptabaseClient = new AptabaseClient ( "A-US-4105716286" , new AptabaseOptions
471479 {
472480 EnablePersistence = true ,
473481#if DEBUG
@@ -477,6 +485,7 @@ private static void Run()
477485#endif
478486 } , logger ) ;
479487 var telemetry = new Telemetry ( DbConnection , aptabaseClient ) ;
488+ aptabaseClientObject = aptabaseClient ;
480489 telemetry . TrackEvent ( "app_started" , new Dictionary < string , object >
481490 {
482491 { "CPU" , SnapXResources . CPU } ,
@@ -586,6 +595,12 @@ public static void CloseSequence()
586595 DbConnection . Dispose ( ) ;
587596 }
588597
598+ if ( TelemetryEnabled ( ) )
599+ {
600+ var client = aptabaseClientObject as AptabaseClient ;
601+ client ! . DisposeAsync ( ) ;
602+ }
603+
589604 DebugHelper . WriteLine ( "SnapX closed." ) ;
590605 DebugHelper . FlushBufferedMessages ( ) ;
591606 Environment . Exit ( 0 ) ;
@@ -612,7 +627,7 @@ private static void UpdatePersonalPath()
612627 {
613628 MigratePersonalPathConfig ( ) ;
614629
615- string customPersonalPath = ReadPersonalPathConfig ( ) ;
630+ string ? customPersonalPath = ReadPersonalPathConfig ( ) ;
616631
617632 if ( ! string . IsNullOrEmpty ( customPersonalPath ) )
618633 {
@@ -650,12 +665,10 @@ private static void UpdatePersonalPath()
650665
651666 private static void CreateParentFolders ( )
652667 {
653- if ( ! Sandbox && Directory . Exists ( PersonalFolder ) )
654- {
655- FileHelpers . CreateDirectory ( SettingManager . SnapshotFolder ) ;
656- FileHelpers . CreateDirectory ( ImageEffectsFolder ) ;
657- FileHelpers . CreateDirectory ( ScreenshotsParentFolder ) ;
658- }
668+ if ( Sandbox || ! Directory . Exists ( PersonalFolder ) ) return ;
669+ FileHelpers . CreateDirectory ( SettingManager . SnapshotFolder ) ;
670+ FileHelpers . CreateDirectory ( ImageEffectsFolder ) ;
671+ FileHelpers . CreateDirectory ( ScreenshotsParentFolder ) ;
659672 }
660673
661674 private static void RegisterIntegrations ( )
@@ -721,7 +734,7 @@ public static string ReadPersonalPathConfig()
721734
722735 public static bool WritePersonalPathConfig ( string path )
723736 {
724- path = path ? . Trim ( ) ?? string . Empty ;
737+ path = path . Trim ( ) ?? string . Empty ;
725738
726739 if ( string . IsNullOrEmpty ( path ) && ! File . Exists ( PersonalPathConfigFilePath ) )
727740 return false ;
0 commit comments