mirror of
https://github.com/MolotovCherry/virtual-display-rs.git
synced 2025-09-04 15:17:53 +00:00
Add logging functionality (especially for exceptions)
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
using Microsoft.UI.Xaml;
|
||||
using Serilog;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using Virtual_Display_Driver_Control.Common;
|
||||
using Virtual_Display_Driver_Control.Helpers;
|
||||
|
||||
@@ -11,6 +15,10 @@ public partial class App : Application {
|
||||
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
|
||||
public App() {
|
||||
Logging.Initialize();
|
||||
|
||||
UnhandledException += App_UnhandledException;
|
||||
AppDomain.CurrentDomain.UnhandledException += Domain_UnhandledException;
|
||||
InitializeComponent();
|
||||
Settings = SettingsProvider.Initialize();
|
||||
}
|
||||
@@ -22,5 +30,56 @@ public partial class App : Application {
|
||||
MaterialHelper.Initialize();
|
||||
|
||||
Window.Activate();
|
||||
Window.Closed += OnClosed;
|
||||
}
|
||||
|
||||
private void OnClosed(object sender, WindowEventArgs e) {
|
||||
// cleanup ops
|
||||
Log.CloseAndFlush();
|
||||
}
|
||||
|
||||
void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e) {
|
||||
_UnhandledException(sender, e.Exception);
|
||||
}
|
||||
|
||||
void Domain_UnhandledException(object sender, System.UnhandledExceptionEventArgs e) {
|
||||
_UnhandledException(sender, (Exception)e.ExceptionObject);
|
||||
}
|
||||
|
||||
async void _UnhandledException(object sender, Exception ex) {
|
||||
StringBuilder formattedException = new StringBuilder() { Capacity = 200 };
|
||||
|
||||
formattedException.Append("\n--------- UNHANDLED EXCEPTION ---------");
|
||||
|
||||
if (ex is not null) {
|
||||
formattedException.Append($"\n>>>> HRESULT: {ex.HResult}\n");
|
||||
if (ex.Message is not null) {
|
||||
formattedException.Append("\n--- MESSAGE ---\n");
|
||||
formattedException.Append(ex.Message);
|
||||
}
|
||||
if (ex.StackTrace is not null) {
|
||||
formattedException.Append("\n--- STACKTRACE ---\n");
|
||||
formattedException.Append(ex.StackTrace);
|
||||
}
|
||||
if (ex.Source is not null) {
|
||||
formattedException.Append("\n--- SOURCE ---\n");
|
||||
formattedException.Append(ex.Source);
|
||||
}
|
||||
if (ex.InnerException is not null) {
|
||||
formattedException.Append("\n--- INNER ---\n");
|
||||
formattedException.Append(ex.InnerException);
|
||||
}
|
||||
} else {
|
||||
formattedException.Append("\nException is null!\n");
|
||||
}
|
||||
|
||||
formattedException.Append("\n---------------------------------------\n");
|
||||
|
||||
Log.Fatal(formattedException.ToString());
|
||||
|
||||
Log.CloseAndFlush();
|
||||
|
||||
// Please check "Output Window" for exception details (View -> Output Window) (CTRL + ALT + O)
|
||||
Debugger.Break();
|
||||
}
|
||||
}
|
||||
|
25
Virtual Display Driver Control/Common/Logging.cs
Normal file
25
Virtual Display Driver Control/Common/Logging.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using Serilog;
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Virtual_Display_Driver_Control.Common;
|
||||
class Logging {
|
||||
public static void Initialize() {
|
||||
var log = new LoggerConfiguration()
|
||||
// Always log to debug regardless
|
||||
.WriteTo.Debug();
|
||||
|
||||
// Write output log if not in debug mode
|
||||
#if !DEBUG
|
||||
log.WriteTo.File(Path.Combine(SettingsProvider.AppDir, "app.log"),
|
||||
rollingInterval: RollingInterval.Day,
|
||||
rollOnFileSizeLimit: true);
|
||||
#endif
|
||||
|
||||
Log.Logger = log.CreateLogger();
|
||||
}
|
||||
|
||||
public static void Dispose() {
|
||||
Log.CloseAndFlush();
|
||||
}
|
||||
}
|
@@ -50,6 +50,9 @@
|
||||
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.231115000" />
|
||||
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.2428" />
|
||||
<PackageReference Include="Octokit" Version="9.0.0" />
|
||||
<PackageReference Include="Serilog" Version="3.1.1" />
|
||||
<PackageReference Include="Serilog.Sinks.Debug" Version="2.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
||||
<PackageReference Include="System.Drawing.Common" Version="8.0.0" />
|
||||
<PackageReference Include="GitVersion.MsBuild" Version="5.12.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
|
Reference in New Issue
Block a user