Skip to content

Commit 25fe950

Browse files
committed
Add Logger
1 parent 00609b9 commit 25fe950

8 files changed

Lines changed: 61 additions & 6 deletions

File tree

dev/DevWinUI.Gallery/App.xaml.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,12 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
5959
MainWindow.AppWindow.SetIcon("Assets/icon.ico");
6060

6161
MainWindow.Activate();
62+
63+
if (Settings.UseDeveloperMode)
64+
{
65+
ConfigureLogger();
66+
}
67+
68+
UnhandledException += (s, e) => Logger?.Error(e.Exception, "UnhandledException");
6269
}
6370
}

dev/DevWinUI.Gallery/Common/AppConfig.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ namespace DevWinUIGallery.Common;
66
[GenerateAutoSaveOnChange]
77
public partial class AppConfig : NotifiyingJsonSettings, IVersionable
88
{
9-
[EnforcedVersion("1.0.0.0")]
10-
public Version Version { get; set; } = new Version(1, 0, 0, 0);
9+
[EnforcedVersion("8.5.0.0")]
10+
public Version Version { get; set; } = new Version(8, 5, 0, 0);
1111

1212
public string fileName { get; set; } = Constants.AppConfigPath;
13-
13+
private bool useDeveloperMode { get; set; }
1414
public string lastUpdateCheck { get; set; }
15-
16-
// Docs: https://github.com/Nucs/JsonSettings
1715
}

dev/DevWinUI.Gallery/Common/Constants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ public static class Constants
44
{
55
public static readonly string RootDirectoryPath = Path.Combine(PathHelper.GetAppDataFolderPath(), ProcessInfoHelper.ProductNameAndVersion);
66
public static readonly string AppConfigPath = Path.Combine(RootDirectoryPath, "AppConfig.json");
7+
public static readonly string LogDirectoryPath = Path.Combine(RootDirectoryPath, "Log");
8+
public static readonly string LogFilePath = Path.Combine(LogDirectoryPath, "Log.txt");
79
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Serilog;
2+
3+
namespace DevWinUIGallery.Common;
4+
public static class LoggerSetup
5+
{
6+
public static ILogger Logger { get; private set; }
7+
8+
public static void ConfigureLogger()
9+
{
10+
if (!Directory.Exists(Constants.LogDirectoryPath))
11+
{
12+
Directory.CreateDirectory(Constants.LogDirectoryPath);
13+
}
14+
15+
Logger = new LoggerConfiguration()
16+
.Enrich.WithProperty("Version", ProcessInfoHelper.Version)
17+
.WriteTo.File(Constants.LogFilePath, rollingInterval: RollingInterval.Day)
18+
.WriteTo.Debug()
19+
.CreateLogger();
20+
}
21+
}
22+

dev/DevWinUI.Gallery/DevWinUI.Gallery.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.6" />
4242
<PackageReference Include="nucs.JsonSettings" Version="2.0.2" />
4343
<PackageReference Include="nucs.JsonSettings.AutosaveGenerator" Version="2.0.4" />
44+
<PackageReference Include="Serilog" Version="4.3.0" />
45+
<PackageReference Include="Serilog.Sinks.Debug" Version="3.0.0" />
46+
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
4447
</ItemGroup>
4548

4649
<!--

dev/DevWinUI.Gallery/GlobalUsings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
global using Microsoft.UI.Xaml.Controls;
1111
global using Microsoft.UI.Xaml.Navigation;
1212
global using static DevWinUIGallery.Common.AppHelper;
13+
global using static DevWinUIGallery.Common.LoggerSetup;

dev/DevWinUI.Gallery/Views/Settings/GeneralSettingPage.xaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,19 @@
1515
VerticalScrollBarVisibility="Auto">
1616
<StackPanel Margin="10"
1717
dev:PanelAttach.ChildrenTransitions="Default"
18-
Spacing="5" />
18+
Spacing="5">
19+
<dev:SettingsExpander Description="By activating this option, if an error or crash occurs, its information will be saved in a file called Log{YYYYMMDD}.txt"
20+
Header="Developer Mode (Restart Required)"
21+
HeaderIcon="{dev:BitmapIcon Source=Assets/Fluent/DevMode.png}">
22+
<ToggleSwitch IsOn="{x:Bind common:AppHelper.Settings.UseDeveloperMode, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
23+
<dev:SettingsExpander.ItemsHeader>
24+
<HyperlinkButton HorizontalAlignment="Stretch"
25+
HorizontalContentAlignment="Left"
26+
Click="NavigateToLogPath_Click"
27+
Content="{x:Bind common:Constants.LogDirectoryPath}" />
28+
</dev:SettingsExpander.ItemsHeader>
29+
</dev:SettingsExpander>
30+
</StackPanel>
1931
</ScrollView>
2032
</Page>
2133

dev/DevWinUI.Gallery/Views/Settings/GeneralSettingPage.xaml.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ public GeneralSettingPage()
88
ViewModel = App.GetService<GeneralSettingViewModel>();
99
this.InitializeComponent();
1010
}
11+
12+
private async void NavigateToLogPath_Click(object sender, RoutedEventArgs e)
13+
{
14+
string folderPath = (sender as HyperlinkButton).Content.ToString();
15+
if (Directory.Exists(folderPath))
16+
{
17+
Windows.Storage.StorageFolder folder = await Windows.Storage.StorageFolder.GetFolderFromPathAsync(folderPath);
18+
await Windows.System.Launcher.LaunchFolderAsync(folder);
19+
}
20+
}
1121
}
1222

1323

0 commit comments

Comments
 (0)