66using LCSC . Discord . Extensions ;
77using LCSC . Discord . Services ;
88using LCTWorks . Telemetry ;
9+ using LCTWorks . WinUI ;
910using LCTWorks . WinUI . Helpers ;
1011using Microsoft . Extensions . Configuration ;
1112using Microsoft . Extensions . DependencyInjection ;
1617using System . Threading . Tasks ;
1718using Windows . ApplicationModel ;
1819
19- namespace LCSC . App
20+ namespace LCSC . App ;
21+
22+ public partial class App : Application , IAppExtended
2023{
21- public partial class App : Application
24+ private readonly ITelemetryService ? _telemetryService ;
25+
26+ public App ( )
2227 {
23- private readonly ITelemetryService ? _telemetryService ;
28+ var configuration = ReadConfigurations ( ) ;
29+ Services = ConfigureServices ( configuration ) ;
30+ BuildHelper . IsDebugBuild = AppHelper . IsDebug ( ) ;
31+ this . InitializeComponent ( ) ;
32+
33+ _telemetryService = Services . GetService < ITelemetryService > ( ) ;
34+ UnhandledException += App_UnhandledException ;
35+ TaskScheduler . UnobservedTaskException += OnUnobservedTaskException ;
36+ AppDomain . CurrentDomain . UnhandledException += CurrentDomain_UnhandledException ;
37+ }
2438
25- public App ( )
26- {
27- var configuration = ReadConfigurations ( ) ;
28- Services = ConfigureServices ( configuration ) ;
29- BuildHelper . IsDebugBuild = AppHelper . IsDebug ( ) ;
30- this . InitializeComponent ( ) ;
31-
32- _telemetryService = Services . GetService < ITelemetryService > ( ) ;
33- UnhandledException += App_UnhandledException ;
34- TaskScheduler . UnobservedTaskException += OnUnobservedTaskException ;
35- AppDomain . CurrentDomain . UnhandledException += CurrentDomain_UnhandledException ;
36- }
39+ public new static App Current => ( App ) Application . Current ;
3740
38- public new static App Current => ( App ) Application . Current ;
41+ public static Window MainWindow { get ; } = new MainWindow ( ) ;
3942
40- public static Window MainWindow { get ; } = new MainWindow ( ) ;
43+ Window IAppExtended . MainWindow => MainWindow ;
4144
42- public IServiceProvider Services { get ; }
45+ public IServiceProvider Services { get ; }
4346
44- protected override void OnLaunched ( LaunchActivatedEventArgs args )
45- {
46- MainWindow . Activate ( ) ;
47- }
47+ protected override void OnLaunched ( LaunchActivatedEventArgs args )
48+ {
49+ MainWindow . Activate ( ) ;
50+ }
51+
52+ private static ServiceProvider ConfigureServices ( IConfiguration configuration )
53+ => new ServiceCollection ( )
54+ //Services
55+ . AddSingleton ( new CacheService ( ApplicationData . GetDefault ( ) . LocalCachePath ) )
56+ . AddSingleton ( sp => new LadderService ( sp . GetRequiredService < CacheService > ( ) , configuration [ "BattleNetSettings:clientId" ] , configuration [ "BattleNetSettings:clientSecret" ] ) )
57+ . AddSingleton ( sp => new CommunityDataService ( sp . GetRequiredService < LadderService > ( ) , sp . GetRequiredService < CacheService > ( ) , configuration [ "AirBaseSettings:token" ] , configuration [ "AirBaseSettings:baseId" ] , Package . Current . InstalledLocation . Path ) )
4858
49- private static ServiceProvider ConfigureServices ( IConfiguration configuration )
50- => new ServiceCollection ( )
51- //Services
52- . AddSingleton ( new CacheService ( ApplicationData . GetDefault ( ) . LocalCachePath ) )
53- . AddSingleton ( sp => new LadderService ( sp . GetRequiredService < CacheService > ( ) , configuration [ "BattleNetSettings:clientId" ] , configuration [ "BattleNetSettings:clientSecret" ] ) )
54- . AddSingleton ( sp => new CommunityDataService ( sp . GetRequiredService < LadderService > ( ) , sp . GetRequiredService < CacheService > ( ) , configuration [ "AirBaseSettings:token" ] , configuration [ "AirBaseSettings:baseId" ] , Package . Current . InstalledLocation . Path ) )
55-
56- //Discord bot
57- . AddLogging ( config =>
58- {
59- config . AddConsole ( ) ;
60- config . AddProvider ( new ConsoleLoggerProvider ( ) ) ;
61- } )
62- . AddSingleton < DiscordBotService > ( )
63- . ConfigureDiscordClient ( configuration [ "DiscordSettings:token" ] )
64-
65- //ViewModels
66- . AddSingleton < MainViewModel > ( )
67- . AddTransient < DiscordBotViewModel > ( )
68- . AddTransient < MembersViewModel > ( )
69- . AddTransient < TournamentsViewModel > ( )
70-
71- //Telemetry
72- . AddSentry ( configuration [ "TelemetryKey:key" ] ?? string . Empty , AppHelper . GetEnvironment ( ) , BuildHelper . IsDebugBuild , RuntimePackageHelper . GetTelemetryContextData ( ) )
73- . AddSerilog ( AppStorageHelper . GetLocalFolder ( "Log" ) . Path , BuildHelper . IsDebugBuild ? Serilog . Events . LogEventLevel . Debug : Serilog . Events . LogEventLevel . Information , BuildHelper . IsDebugBuild , includeConsole : true )
74-
75- //Build:
76- . BuildServiceProvider ( true ) ;
77-
78- private static IConfiguration ReadConfigurations ( )
59+ //Discord bot
60+ . AddLogging ( config =>
7961 {
80- return new ConfigurationBuilder ( )
81- . SetBasePath ( Package . Current . InstalledLocation . Path )
82- . AddJsonFile ( "assets\\ Config\\ appsettings.json" , false )
83- . Build ( ) ;
84- }
62+ config . AddConsole ( ) ;
63+ config . AddProvider ( new ConsoleLoggerProvider ( ) ) ;
64+ } )
65+ . AddSingleton < DiscordBotService > ( )
66+ . ConfigureDiscordClient ( configuration [ "DiscordSettings:token" ] )
67+
68+ //ViewModels
69+ . AddSingleton < MainViewModel > ( )
70+ . AddTransient < DiscordBotViewModel > ( )
71+ . AddTransient < MembersViewModel > ( )
72+ . AddTransient < TournamentsViewModel > ( )
73+
74+ //Telemetry
75+ . AddSentry ( configuration [ "TelemetryKey:key" ] ?? string . Empty , AppHelper . GetEnvironment ( ) , BuildHelper . IsDebugBuild , RuntimePackageHelper . GetTelemetryContextData ( ) )
76+ . AddSerilog ( AppStorageHelper . GetLocalFolder ( "Log" ) . Path , BuildHelper . IsDebugBuild ? Serilog . Events . LogEventLevel . Debug : Serilog . Events . LogEventLevel . Information , BuildHelper . IsDebugBuild , includeConsole : true )
77+
78+ //Build:
79+ . BuildServiceProvider ( true ) ;
80+
81+ private static IConfiguration ReadConfigurations ( )
82+ {
83+ return new ConfigurationBuilder ( )
84+ . SetBasePath ( Package . Current . InstalledLocation . Path )
85+ . AddJsonFile ( "assets\\ Config\\ appsettings.json" , false )
86+ . Build ( ) ;
87+ }
8588
86- private void App_UnhandledException ( object sender , Microsoft . UI . Xaml . UnhandledExceptionEventArgs e )
87- => _telemetryService ? . ReportUnhandledException ( e . Exception ) ;
89+ private void App_UnhandledException ( object sender , Microsoft . UI . Xaml . UnhandledExceptionEventArgs e )
90+ => _telemetryService ? . ReportUnhandledException ( e . Exception ) ;
8891
89- private void CurrentDomain_UnhandledException ( object sender , System . UnhandledExceptionEventArgs e )
90- => _telemetryService ? . ReportUnhandledException ( e . ExceptionObject as Exception ?? new Exception ( "Unknown exception" ) ) ;
92+ private void CurrentDomain_UnhandledException ( object sender , System . UnhandledExceptionEventArgs e )
93+ => _telemetryService ? . ReportUnhandledException ( e . ExceptionObject as Exception ?? new Exception ( "Unknown exception" ) ) ;
9194
92- private void OnUnobservedTaskException ( object ? sender , UnobservedTaskExceptionEventArgs ? e )
95+ private void OnUnobservedTaskException ( object ? sender , UnobservedTaskExceptionEventArgs ? e )
96+ {
97+ if ( e ? . Exception == null )
98+ {
99+ return ;
100+ }
101+ var flattenedExceptions = e . Exception . Flatten ( ) . InnerExceptions ;
102+ foreach ( var exception in flattenedExceptions )
93103 {
94- if ( e ? . Exception == null )
95- {
96- return ;
97- }
98- var flattenedExceptions = e . Exception . Flatten ( ) . InnerExceptions ;
99- foreach ( var exception in flattenedExceptions )
100- {
101- _telemetryService ? . LogAndTrackError ( GetType ( ) , exception ) ;
102- }
103- e . SetObserved ( ) ;
104+ _telemetryService ? . LogAndTrackError ( GetType ( ) , exception ) ;
104105 }
106+ e . SetObserved ( ) ;
105107 }
106108}
0 commit comments