From 65a0190732bd13c25fee2e5ce18ed6050255c8e1 Mon Sep 17 00:00:00 2001 From: Bin Date: Thu, 20 Feb 2025 00:02:45 +0800 Subject: [PATCH] feat(logging): add Serilog for enhanced logging capabilities (#41) 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. --- Directory.Packages.props | 5 +++++ SketchNow/App.xaml.cs | 19 ++++++++++++++++--- SketchNow/SketchNow.csproj | 8 ++++++++ SketchNow/appsettings.json | 20 ++++++++++++++++++++ 4 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 SketchNow/appsettings.json diff --git a/Directory.Packages.props b/Directory.Packages.props index b5c49a4..a06d142 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -27,9 +27,14 @@ + + + + + diff --git a/SketchNow/App.xaml.cs b/SketchNow/App.xaml.cs index 616189e..710835b 100644 --- a/SketchNow/App.xaml.cs +++ b/SketchNow/App.xaml.cs @@ -12,6 +12,8 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Serilog; + using SingleInstanceCore; using SketchNow.ViewModels; @@ -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(); services.AddSingleton(); services.AddSingleton(); @@ -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) @@ -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"); } } @@ -145,6 +154,8 @@ 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) @@ -152,6 +163,8 @@ static void TaskSchedulerUnobservedTaskException(object? sender, UnobservedTaskE 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 diff --git a/SketchNow/SketchNow.csproj b/SketchNow/SketchNow.csproj index 9e97fda..e49b640 100644 --- a/SketchNow/SketchNow.csproj +++ b/SketchNow/SketchNow.csproj @@ -49,6 +49,11 @@ + + + + + @@ -83,6 +88,9 @@ + + PreserveNewest + SettingsSingleFileGenerator Settings.Designer.cs diff --git a/SketchNow/appsettings.json b/SketchNow/appsettings.json new file mode 100644 index 0000000..bf2ed2b --- /dev/null +++ b/SketchNow/appsettings.json @@ -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" + } + ] + } +} \ No newline at end of file