Skip to content

Commit

Permalink
feat(logging): add Serilog for enhanced logging capabilities (#41)
Browse files Browse the repository at this point in the history
Updated `Directory.Packages.props` and `SketchNow.csproj` to include new package references for Microsoft.Extensions.Logging and Serilog. Modified `App.xaml.cs` to configure logging services and handle unhandled exceptions with Serilog. Added `appsettings.json` for logging configuration, specifying minimum logging level and file sink settings.
  • Loading branch information
realybin committed Feb 19, 2025
1 parent 25743cd commit f4bbb3f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@
<PackageVersion Include="MaterialDesignThemes" Version="5.2.1" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.2" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="9.0.2" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="9.0.2" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="Moq.AutoMock" Version="3.5.0" />
<PackageVersion Include="Serilog.Extensions.Hosting" Version="9.0.0" />
<PackageVersion Include="Serilog.Extensions.Logging" Version="9.0.0" />
<PackageVersion Include="Serilog.Settings.Configuration" Version="9.0.0" />
<PackageVersion Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageVersion Include="SingleInstanceCore" Version="2.2.2" />
<PackageVersion Include="System.Text.Json" Version="9.0.2" />
<PackageVersion Include="ValueConverters" Version="3.1.22" />
Expand Down
19 changes: 16 additions & 3 deletions SketchNow/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using Serilog;

using SingleInstanceCore;

using SketchNow.ViewModels;
Expand Down Expand Up @@ -56,9 +58,12 @@ private static async Task MainAsync(string[] args)
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostBuilderContext, configurationBuilder)
=> configurationBuilder.AddUserSecrets(typeof(App).Assembly))
=> configurationBuilder.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddUserSecrets(typeof(App).Assembly))
.ConfigureServices((hostContext, services) =>
{
services.AddLogging(configure => configure.AddSerilog(new LoggerConfiguration().ReadFrom.Configuration(hostContext.Configuration).CreateLogger()));

services.AddSingleton<MainWindow>();
services.AddSingleton<MainWindowViewModel>();
services.AddSingleton<SettingsViewModel>();
Expand All @@ -80,8 +85,8 @@ public void OnInstanceInvoked(string[] args) { }
#if !DEBUG
public App()
{
this.Startup += Application_Startup;
this.Exit += Application_Exit;
Startup += Application_Startup;
Exit += Application_Exit;
}

void Application_Startup(object sender, StartupEventArgs e)
Expand Down Expand Up @@ -118,11 +123,15 @@ static void AppDispatcherUnhandledException(object sender, DispatcherUnhandledEx
MessageBox.Show(
$"UI thread meets an exception: {e.Exception.Message}{Environment.NewLine}{e.Exception.StackTrace}",
"Unhandled Exception", MessageBoxButton.OK, MessageBoxImage.Error);

Log.Error(e.Exception, "UI thread meets an exception");
}
catch (Exception ex)
{
MessageBox.Show($"UI thread meets a fatal exception! {ex.Message}{Environment.NewLine}{ex.StackTrace}",
"Unhandled Exception", MessageBoxButton.OK, MessageBoxImage.Error);

Log.Error(ex, "UI thread meets a fatal exception");
}
}

Expand All @@ -145,13 +154,17 @@ static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEve
}

MessageBox.Show(sbEx.ToString());

Log.Error(e.ExceptionObject as Exception, "Non-UI thread meets exception");
}

static void TaskSchedulerUnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
{
MessageBox.Show(
$"Task thread meets exception:{e.Exception.Message}{Environment.NewLine}{e.Exception.StackTrace}",
"Unobserved Task", MessageBoxButton.OK, MessageBoxImage.Error);

Log.Error(e.Exception, "Task thread meets exception");
e.SetObserved();
}
#endif
Expand Down
8 changes: 8 additions & 0 deletions SketchNow/SketchNow.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
<PackageReference Include="MaterialDesignThemes" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Serilog.Extensions.Hosting" />
<PackageReference Include="Serilog.Extensions.Logging" />
<PackageReference Include="Serilog.Settings.Configuration" />
<PackageReference Include="Serilog.Sinks.File" />
<PackageReference Include="SingleInstanceCore" />
<PackageReference Include="System.Text.Json" />
<PackageReference Include="ValueConverters" />
Expand Down Expand Up @@ -83,6 +88,9 @@
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
20 changes: 20 additions & 0 deletions SketchNow/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"Serilog": {
"MinimumLevel": "Information",
"Using": [
"Serilog.Sinks.File"
],
"WriteTo": [
{
"Args": {
"buffered": false,
"fileSizeLimitBytes": 10240000,
"path": "logs/.log",
"retainedFileCountLimit": 7,
"rollingInterval": "Day"
},
"Name": "File"
}
]
}
}

0 comments on commit f4bbb3f

Please sign in to comment.