From 83bdcd36e2037725813d9dabf5b68249448d9c9e Mon Sep 17 00:00:00 2001 From: Anna Novikova Date: Tue, 8 Oct 2024 19:25:43 +0200 Subject: [PATCH] Added Serilog logging and an example of its usage for exceptions. --- src/SmartCommander/Program.cs | 9 +++++++++ src/SmartCommander/SmartCommander.csproj | 5 ++++- .../ViewModels/FileSearchViewModel.cs | 16 ++++++++-------- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/SmartCommander/Program.cs b/src/SmartCommander/Program.cs index a80e428..3495bc3 100644 --- a/src/SmartCommander/Program.cs +++ b/src/SmartCommander/Program.cs @@ -1,5 +1,6 @@ using Avalonia; using Avalonia.ReactiveUI; +using Serilog; using SmartCommander.Models; using System; using System.Diagnostics; @@ -35,6 +36,14 @@ public static void Main(string[] args) } } else { + Log.Logger = new LoggerConfiguration() + .MinimumLevel.Debug() +#if DEBUG + .WriteTo.Console() +#endif + .WriteTo.File("log.txt", rollingInterval: RollingInterval.Day) + .CreateLogger(); + BuildAvaloniaApp().StartWithClassicDesktopLifetime(args); } diff --git a/src/SmartCommander/SmartCommander.csproj b/src/SmartCommander/SmartCommander.csproj index 049823c..45f9823 100644 --- a/src/SmartCommander/SmartCommander.csproj +++ b/src/SmartCommander/SmartCommander.csproj @@ -51,7 +51,10 @@ - + + + + diff --git a/src/SmartCommander/ViewModels/FileSearchViewModel.cs b/src/SmartCommander/ViewModels/FileSearchViewModel.cs index 6dcdf74..c42c3af 100644 --- a/src/SmartCommander/ViewModels/FileSearchViewModel.cs +++ b/src/SmartCommander/ViewModels/FileSearchViewModel.cs @@ -1,5 +1,6 @@ using Avalonia.Threading; using ReactiveUI; +using Serilog; using SmartCommander.Extensions; using SmartCommander.ViewModels; using System; @@ -74,19 +75,18 @@ public async Task SearchAsync(string folderPath, string searchPattern, Can cancellationToken.ThrowIfCancellationRequested(); await SearchAsync(subDir, searchPattern, cancellationToken); } - } - // TODO: add logging - /* - catch (OperationCanceledException) + } + catch (OperationCanceledException e) { + Log.Error("OperationCanceledException: " + e.Message); } catch (UnauthorizedAccessException e) { - } - */ - catch (Exception) + Log.Error("UnauthorizedAccessException: " + e.Message); + } + catch (Exception e) { - + Log.Error("Exception: " + e.Message); } return true;