Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(logging): add Serilog for enhanced logging capabilities #41

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 9 additions & 1 deletion SketchNow/SketchNow.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<UseWPF>true</UseWPF>
<StartupObject>SketchNow.App</StartupObject>
<ApplicationIcon>Resources\AppIcon.ico</ApplicationIcon>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
<RepositoryUrl>https://github.com/SketchNow/SketchNow.WPF</RepositoryUrl>

<IncludeMaterialDesignFont>False</IncludeMaterialDesignFont>
Expand Down 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"
}
]
}
}
Loading