Skip to content

Commit 976f93a

Browse files
author
lampenlampen
authored
NLog (#426)
1 parent 066ba17 commit 976f93a

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

Files/App.xaml.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using Files.CommandLine;
2626
using Files.View_Models;
2727
using Files.Controls;
28+
using NLog;
2829

2930
namespace Files
3031
{
@@ -56,23 +57,40 @@ public static IShellPage CurrentInstance
5657
public static ObservableCollection<WSLDistroItem> linuxDistroItems = new ObservableCollection<WSLDistroItem>();
5758
public static SettingsViewModel AppSettings { get; set; }
5859

60+
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
61+
5962
public App()
6063
{
6164
this.InitializeComponent();
6265
this.Suspending += OnSuspending;
66+
67+
// Initialize NLog
68+
Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
69+
NLog.LogManager.Configuration.Variables["LogPath"] = storageFolder.Path;
70+
71+
RegisterUncaughtExceptionLogger();
72+
6373
consentDialog = new Dialogs.ConsentDialog();
6474
propertiesDialog = new Dialogs.PropertiesDialog();
6575
layoutDialog = new Dialogs.LayoutDialog();
6676
addItemDialog = new Dialogs.AddItemDialog();
6777
exceptionDialog = new Dialogs.ExceptionDialog();
68-
//this.UnhandledException += App_UnhandledException;
78+
// this.UnhandledException += App_UnhandledException;
6979
Clipboard.ContentChanged += Clipboard_ContentChanged;
7080
Clipboard_ContentChanged(null, null);
7181
AppCenter.Start("682666d1-51d3-4e4a-93d0-d028d43baaa0", typeof(Analytics), typeof(Crashes));
7282

7383
AppSettings = new SettingsViewModel();
7484
}
7585

86+
private void RegisterUncaughtExceptionLogger()
87+
{
88+
UnhandledException += (sender, args) =>
89+
{
90+
Logger.Error(args.Exception, args.Message);
91+
};
92+
}
93+
7694
private void CoreWindow_PointerPressed(CoreWindow sender, PointerEventArgs args)
7795
{
7896
if (args.CurrentPoint.Properties.IsXButton1Pressed)
@@ -172,7 +190,9 @@ public static IReadOnlyList<ContentDialog> FindDisplayedContentDialogs<T>()
172190
/// <param name="e">Details about the launch request and process.</param>
173191
protected override void OnLaunched(LaunchActivatedEventArgs e)
174192
{
175-
bool canEnablePrelaunch = Windows.Foundation.Metadata.ApiInformation.IsMethodPresent("Windows.ApplicationModel.Core.CoreApplication", "EnablePrelaunch");
193+
Logger.Info("App launched");
194+
195+
bool canEnablePrelaunch = Windows.Foundation.Metadata.ApiInformation.IsMethodPresent("Windows.ApplicationModel.Core.CoreApplication", "EnablePrelaunch");
176196

177197
Frame rootFrame = Window.Current.Content as Frame;
178198

@@ -220,6 +240,8 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
220240

221241
protected override void OnActivated(IActivatedEventArgs args)
222242
{
243+
Logger.Info("App activated");
244+
223245
// Window management
224246
Frame rootFrame = Window.Current.Content as Frame;
225247
if (rootFrame == null)

Files/Files.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@
278278
<None Include="Assets\terminal\terminal.json">
279279
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
280280
</None>
281+
<None Include="NLog.config">
282+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
283+
</None>
281284
<None Include="Package.StoreAssociation.xml" />
282285
<Content Include="Assets\QuickLook\quicklook_icon_black.png" />
283286
<Content Include="Assets\WSL\alpine.svg" />
@@ -466,6 +469,12 @@
466469
<PackageReference Include="Newtonsoft.Json">
467470
<Version>12.0.3</Version>
468471
</PackageReference>
472+
<PackageReference Include="NLog">
473+
<Version>4.6.8</Version>
474+
</PackageReference>
475+
<PackageReference Include="NLog.Schema">
476+
<Version>4.6.8</Version>
477+
</PackageReference>
469478
</ItemGroup>
470479
<ItemGroup>
471480
<SDKReference Include="WindowsDesktop, Version=10.0.18362.0">

Files/NLog.config

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
5+
<targets>
6+
<target name="logfile" xsi:type="File" fileName="${var:LogPath}\debug.txt" concurrentWrites="true" />
7+
<target name="logconsole" xsi:type="Console" />
8+
</targets>
9+
10+
<rules>
11+
<logger name="*" minlevel="Debug" writeTo="logconsole" />
12+
<logger name="*" minlevel="Debug" writeTo="logfile" />
13+
</rules>
14+
</nlog>

0 commit comments

Comments
 (0)