Skip to content

Commit

Permalink
Added Serilog logging and an example of its usage for exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
anovik committed Oct 8, 2024
1 parent 1b60e5f commit 83bdcd3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/SmartCommander/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Avalonia;
using Avalonia.ReactiveUI;
using Serilog;
using SmartCommander.Models;
using System;
using System.Diagnostics;
Expand Down Expand Up @@ -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);
}

Expand Down
5 changes: 4 additions & 1 deletion src/SmartCommander/SmartCommander.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.10" />
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="11.0.10.9" />
<PackageReference Include="MessageBox.Avalonia" Version="3.1.5.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog" Version="4.0.2" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<Compile Update="Assets\Resources.Designer.cs">
Expand Down
16 changes: 8 additions & 8 deletions src/SmartCommander/ViewModels/FileSearchViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Avalonia.Threading;
using ReactiveUI;
using Serilog;
using SmartCommander.Extensions;
using SmartCommander.ViewModels;
using System;
Expand Down Expand Up @@ -74,19 +75,18 @@ public async Task<bool> 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;
Expand Down

0 comments on commit 83bdcd3

Please sign in to comment.