Skip to content

Commit 014d34d

Browse files
authored
Code Quality: Additional logs to troubleshoot crashes (#17955)
1 parent f56c965 commit 014d34d

File tree

7 files changed

+69
-2
lines changed

7 files changed

+69
-2
lines changed

src/Files.App/App.xaml.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ async Task ActivateAsync()
160160
public async Task OnActivatedAsync(AppActivationArguments activatedEventArgs)
161161
{
162162
var activatedEventArgsData = activatedEventArgs.Data;
163-
163+
164164
// Logger may not be initialized yet due to race condition during startup
165165
if (Logger is not null)
166166
Logger.LogInformation($"The app is being activated. Activation type: {activatedEventArgsData.GetType().Name}");
@@ -175,6 +175,8 @@ await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(()
175175
/// </summary>
176176
private void Window_Activated(object sender, WindowActivatedEventArgs args)
177177
{
178+
Logger.LogInformation($"Window_Activated: State={args?.WindowActivationState.ToString()}");
179+
178180
AppModel.IsMainWindowClosed = false;
179181

180182
// TODO(s): Is this code still needed?
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Files Community
2+
// Licensed under the MIT License.
3+
4+
using System.IO;
5+
using System.Security.Cryptography;
6+
using System.Text;
7+
8+
namespace Files.App.Helpers
9+
{
10+
public static class LogPathHelper
11+
{
12+
public static string GetPathIdentifier(string? path)
13+
{
14+
if (string.IsNullOrEmpty(path))
15+
return "[Empty]";
16+
17+
try
18+
{
19+
using var md5 = MD5.Create();
20+
var hashBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(path));
21+
22+
//4 bytes, still low collision
23+
var shortHash = BitConverter.ToString(hashBytes, 0, 4).Replace("-", "").ToLowerInvariant();
24+
25+
var extension = Path.GetExtension(path);
26+
27+
if (!string.IsNullOrEmpty(extension))
28+
return $"[hash:{shortHash}{extension}]";
29+
else
30+
return $"[hash:{shortHash}]";
31+
}
32+
catch
33+
{
34+
return "[?]";
35+
}
36+
}
37+
}
38+
}

src/Files.App/ViewModels/ShellViewModel.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,8 @@ private async Task RapidAddItemsToCollectionAsync(string? path, LibraryItem? lib
16941694

16951695
public void CloseWatcher()
16961696
{
1697+
App.Logger.LogInformation($"CloseWatcher: aProcessQueueAction={aProcessQueueAction?.Status.ToString()}, gitProcessQueueAction={gitProcessQueueAction?.Status.ToString()}");
1698+
16971699
watcher?.Dispose();
16981700
watcher = null;
16991701

@@ -2771,6 +2773,8 @@ public void CancelSearch()
27712773

27722774
public void UpdateDateDisplay(bool isFormatChange)
27732775
{
2776+
App.Logger.LogDebug($"UpdateDateDisplay: isFormatChange={isFormatChange}, itemCount={filesAndFolders?.Count}");
2777+
27742778
filesAndFolders.ToList().AsParallel().ForAll(async item =>
27752779
{
27762780
// Reassign values to update date display
@@ -2792,6 +2796,8 @@ public void UpdateDateDisplay(bool isFormatChange)
27922796
public void Dispose()
27932797
{
27942798
CancelLoadAndClearFiles();
2799+
App.Logger.LogInformation($"ShellViewModel.Dispose: CurrentFolder={LogPathHelper.GetPathIdentifier(CurrentFolder?.ItemPath)}");
2800+
27952801
StorageTrashBinService.Watcher.ItemAdded -= RecycleBinItemCreatedAsync;
27962802
StorageTrashBinService.Watcher.ItemDeleted -= RecycleBinItemDeletedAsync;
27972803
StorageTrashBinService.Watcher.RefreshRequested -= RecycleBinRefreshRequestedAsync;

src/Files.App/ViewModels/UserControls/InfoPaneViewModel.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,13 @@ public bool ShowCloudItemButton
115115
public UIElement PreviewPaneContent
116116
{
117117
get => previewPaneContent;
118-
set => SetProperty(ref previewPaneContent, value);
118+
set
119+
{
120+
var oldType = previewPaneContent?.GetType()?.Name;
121+
var newType = value?.GetType()?.Name;
122+
App.Logger.LogDebug($"PreviewPaneContent changing: {oldType} -> {newType}");
123+
SetProperty(ref previewPaneContent, value);
124+
}
119125
}
120126

121127
public bool LoadTagsList

src/Files.App/ViewModels/UserControls/Previews/ShellPreviewViewModel.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

4+
using Files.App.Helpers;
45
using Files.App.ViewModels.Properties;
6+
using Microsoft.Extensions.Logging;
57
using Microsoft.UI.Content;
68
using Microsoft.UI.Xaml;
79
using Microsoft.UI.Xaml.Hosting;
@@ -127,6 +129,8 @@ private unsafe LRESULT WndProc(HWND hwnd, uint msg, WPARAM wParam, LPARAM lParam
127129

128130
public unsafe void LoadPreview(UIElement presenter)
129131
{
132+
App.Logger.LogInformation($"ShellPreview.LoadPreview: Item={LogPathHelper.GetPathIdentifier(Item?.ItemPath)}");
133+
130134
var parent = MainWindow.Instance.WindowHandle;
131135
var hInst = PInvoke.GetModuleHandle(default(PWSTR));
132136
var szClassName = $"{nameof(ShellPreviewViewModel)}-{Guid.NewGuid()}";
@@ -251,7 +255,13 @@ private unsafe bool ChildWindowToXaml(nint parent, UIElement presenter)
251255
public void UnloadPreview()
252256
{
253257
if (_hWnd != HWND.Null)
258+
{
254259
PInvoke.DestroyWindow(_hWnd);
260+
App.Logger.LogInformation($"ShellPreview.UnloadPreview: HWND={((nint)_hWnd)}, Item={LogPathHelper.GetPathIdentifier(Item?.ItemPath)}");
261+
}
262+
else
263+
App.Logger.LogInformation($"ShellPreview.UnloadPreview: HWND=, Item={LogPathHelper.GetPathIdentifier(Item?.ItemPath)}");
264+
255265

256266
_contentExternalOutputLink?.Dispose();
257267
_contentExternalOutputLink = null;

src/Files.App/Views/MainPage.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ private void UpdateDateDisplayTimer_Tick(object sender, object e)
313313
{
314314
if (!App.AppModel.IsMainWindowClosed)
315315
InfoPane?.ViewModel.UpdateDateDisplay();
316+
else
317+
App.Logger.LogWarning("UpdateDateDisplayTimer_Tick: Timer firing after window closed!");
316318
}
317319

318320
private void Page_SizeChanged(object sender, SizeChangedEventArgs e)

src/Files.App/Views/ShellPanesPage.xaml.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using Files.App.Controls;
5+
using Microsoft.Extensions.Logging;
56
using Microsoft.UI.Input;
67
using Microsoft.UI.Xaml;
78
using Microsoft.UI.Xaml.Controls;
@@ -777,6 +778,8 @@ private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
777778

778779
public void Dispose()
779780
{
781+
App.Logger.LogInformation($"ShellPanesPage.Dispose: PaneCount={GetPaneCount()}, ActivePane={LogPathHelper.GetPathIdentifier(ActivePane?.TabBarItemParameter?.NavigationParameter?.ToString())}");
782+
780783
MainWindow.Instance.SizeChanged -= MainWindow_SizeChanged;
781784

782785
// Dispose panes

0 commit comments

Comments
 (0)