3232using Microsoft . AppCenter . Crashes ;
3333using Microsoft . Extensions . DependencyInjection ;
3434using Microsoft . Extensions . Hosting ;
35+ using Microsoft . Extensions . Logging ;
3536using Microsoft . UI . Windowing ;
3637using Microsoft . UI . Xaml ;
3738using Microsoft . UI . Xaml . Controls ;
3839using Microsoft . Windows . AppLifecycle ;
3940using System ;
4041using System . Diagnostics ;
42+ using System . IO ;
4143using System . Linq ;
4244using System . Text ;
4345using System . Threading . Tasks ;
4648using Windows . Storage ;
4749using Windows . UI . Notifications ;
4850
49- // To learn more about WinUI, the WinUI project structure,
50- // and more about our project templates, see: http://aka.ms/winui-project-info.
5151
5252namespace Files . App
5353{
@@ -72,7 +72,6 @@ public partial class App : Application
7272 public static FileTagsManager FileTagsManager { get ; private set ; }
7373
7474 public static ILogger Logger { get ; private set ; }
75- private static readonly UniversalLogWriter logWriter = new UniversalLogWriter ( ) ;
7675 public static SecondaryTileHelper SecondaryTileHelper { get ; private set ; } = new SecondaryTileHelper ( ) ;
7776
7877 public static string AppVersion = $ "{ Package . Current . Id . Version . Major } .{ Package . Current . Id . Version . Minor } .{ Package . Current . Id . Version . Build } .{ Package . Current . Id . Version . Revision } ";
@@ -86,9 +85,6 @@ public partial class App : Application
8685 /// </summary>
8786 public App ( )
8887 {
89- // Initialize logger
90- Logger = new Logger ( logWriter ) ;
91-
9288 UnhandledException += OnUnhandledException ;
9389 TaskScheduler . UnobservedTaskException += OnUnobservedException ;
9490 InitializeComponent ( ) ;
@@ -119,7 +115,7 @@ private static Task StartAppCenter()
119115 }
120116 catch ( Exception ex )
121117 {
122- Logger . Warn ( ex , "AppCenter could not be started." ) ;
118+ App . Logger . LogWarning ( ex , "AppCenter could not be started." ) ;
123119 }
124120
125121 return Task . CompletedTask ;
@@ -174,16 +170,19 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
174170 {
175171 var activatedEventArgs = Microsoft . Windows . AppLifecycle . AppInstance . GetCurrent ( ) . GetActivatedEventArgs ( ) ;
176172
177- Task . Run ( async ( ) => await logWriter . InitializeAsync ( "debug.log" ) ) ;
178- Logger . Info ( $ "App launched. Launch args type: { activatedEventArgs . Data . GetType ( ) . Name } ") ;
179-
173+ var logPath = Path . Combine ( ApplicationData . Current . LocalFolder . Path , "debug.log" ) ;
180174 //start tracking app usage
181175 if ( activatedEventArgs . Data is Windows . ApplicationModel . Activation . IActivatedEventArgs iaea )
182176 SystemInformation . Instance . TrackAppUse ( iaea ) ;
183177
184178 // Initialize MainWindow here
185179 EnsureWindowIsInitialized ( ) ;
186180 host = Host . CreateDefaultBuilder ( )
181+ . ConfigureLogging ( builder =>
182+ builder
183+ . AddProvider ( new FileLoggerProvider ( logPath ) )
184+ . SetMinimumLevel ( Microsoft . Extensions . Logging . LogLevel . Information )
185+ )
187186 . ConfigureServices ( services =>
188187 services
189188 . AddSingleton < IUserSettingsService , UserSettingsService > ( )
@@ -201,7 +200,6 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
201200 . AddSingleton < IDisplayPageContext , DisplayPageContext > ( )
202201 . AddSingleton < IWindowContext , WindowContext > ( )
203202 . AddSingleton < IMultitaskingContext , MultitaskingContext > ( )
204- . AddSingleton ( Logger )
205203 . AddSingleton < IDialogService , DialogService > ( )
206204 . AddSingleton < IImageService , ImagingService > ( )
207205 . AddSingleton < IThreadingService , ThreadingService > ( )
@@ -235,10 +233,13 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
235233 . AddSingleton < AppearanceViewModel > ( )
236234 )
237235 . Build ( ) ;
236+ Logger = host . Services . GetRequiredService < ILogger < App > > ( ) ;
237+ App . Logger . LogInformation ( $ "App launched. Launch args type: { activatedEventArgs . Data . GetType ( ) . Name } ") ;
238+
238239 Ioc . Default . ConfigureServices ( host . Services ) ;
239240 EnsureSettingsAndConfigurationAreBootstrapped ( ) ;
240241
241- _ = InitializeAppComponentsAsync ( ) . ContinueWith ( t => Logger . Warn ( t . Exception , "Error during InitializeAppComponentsAsync()" ) , TaskContinuationOptions . OnlyOnFaulted ) ;
242+ _ = InitializeAppComponentsAsync ( ) . ContinueWith ( t => Logger . LogWarning ( t . Exception , "Error during InitializeAppComponentsAsync()" ) , TaskContinuationOptions . OnlyOnFaulted ) ;
242243
243244 _ = Window . InitializeApplication ( activatedEventArgs . Data ) ;
244245 }
@@ -263,7 +264,7 @@ private void Window_Activated(object sender, WindowActivatedEventArgs args)
263264
264265 public void OnActivated ( AppActivationArguments activatedEventArgs )
265266 {
266- Logger . Info ( $ "App activated. Activated args type: { activatedEventArgs . Data . GetType ( ) . Name } ") ;
267+ App . Logger . LogInformation ( $ "App activated. Activated args type: { activatedEventArgs . Data . GetType ( ) . Name } ") ;
267268 var data = activatedEventArgs . Data ;
268269 // InitializeApplication accesses UI, needs to be called on UI thread
269270 _ = Window . DispatcherQueue . EnqueueAsync ( ( ) => Window . InitializeApplication ( data ) ) ;
@@ -391,7 +392,7 @@ private static void AppUnhandledException(Exception ex, bool shouldShowNotificat
391392 Debugger . Break ( ) ; // Please check "Output Window" for exception details (View -> Output Window) (CTRL + ALT + O)
392393
393394 SaveSessionTabs ( ) ;
394- Logger . UnhandledError ( ex , ex . Message ) ;
395+ App . Logger . LogError ( ex , ex . Message ) ;
395396
396397 if ( ! ShowErrorNotification || ! shouldShowNotification )
397398 return ;
0 commit comments