From 0ba44d3f818d7b74e55ec1bc093a514e67fb4764 Mon Sep 17 00:00:00 2001 From: BoiHanny Date: Sat, 18 May 2024 15:36:07 +0200 Subject: [PATCH] Refactor logging and update project version Refactored the logging mechanism in `Logging.cs` under `vrcosc_magicchatbox.Classes.DataAndSecurity` namespace. Replaced `ShowMSGBox` method calls in `catch` blocks of `WriteException`, `WriteInfo`, and `WriteDebug` methods with a new centralized error handling method `HandleLoggingError`. This method attempts to log errors using NLog and falls back to console logging if NLog fails. Also, added a `try-catch` block around `WriteDebug` method to handle potential exceptions. Updated the `MagicChatbox.csproj` file, incrementing the version number from `0.8.875` to `0.8.878`. --- .../Classes/DataAndSecurity/Logging.cs | 43 ++++++++++++++----- vrcosc-magicchatbox/MagicChatbox.csproj | 2 +- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/vrcosc-magicchatbox/Classes/DataAndSecurity/Logging.cs b/vrcosc-magicchatbox/Classes/DataAndSecurity/Logging.cs index 982caeaf..ab0b4f93 100644 --- a/vrcosc-magicchatbox/Classes/DataAndSecurity/Logging.cs +++ b/vrcosc-magicchatbox/Classes/DataAndSecurity/Logging.cs @@ -25,10 +25,9 @@ public static void ShowMSGBox( } catch (Exception e) { - MessageBox.Show($"Error in ShowMSGBox\n{e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + HandleLoggingError("Error in ShowMSGBox", e); Environment.Exit(10); } - } // Log an exception and optionally show a message box and/or exit the application @@ -53,11 +52,10 @@ public static void WriteException( } catch (Exception e) { - ShowMSGBox(msgboxtext: $"Error in WriteException\n{e.Message}", autoClose: true); + HandleLoggingError("Error in WriteException", e); if (exitapp) Environment.Exit(10); } - } // Log an informational message and optionally show a message box and/or exit the application @@ -73,21 +71,44 @@ public static void WriteInfo(string info, bool MSGBox = false, bool autoclose = } catch (Exception e) { - ShowMSGBox(msgboxtext: $"Error in WriteInfo\n{e.Message}", autoClose: true); + HandleLoggingError("Error in WriteInfo", e); if (exitapp) Environment.Exit(10); } - } // Log a debug message and optionally show a message box and/or exit the application public static void WriteDebug(string debug, bool MSGBox = false, bool autoclose = false, bool exitapp = false) { - LogController.Debug(debug); - if (MSGBox) - ShowMSGBox(msgboxtext: debug, autoClose: autoclose); - if (exitapp) - ShowMSGBox(msgboxtext: "debug did throw application exit", autoClose: autoclose); + try + { + LogController.Debug(debug); + if (MSGBox) + ShowMSGBox(msgboxtext: debug, autoClose: autoclose); + if (exitapp) + Environment.Exit(10); + } + catch (Exception e) + { + HandleLoggingError("Error in WriteDebug", e); + if (exitapp) + Environment.Exit(10); + } + } + + // Centralized method to handle errors during logging + private static void HandleLoggingError(string context, Exception e) + { + try + { + // Attempt to log the error to NLog + LogController.Error($"{context}\n{e.Message}\n{e.StackTrace}"); + } + catch + { + // If NLog fails, fallback to console logging + Console.Error.WriteLine($"{context}\n{e.Message}\n{e.StackTrace}"); + } } } } diff --git a/vrcosc-magicchatbox/MagicChatbox.csproj b/vrcosc-magicchatbox/MagicChatbox.csproj index ab13a85f..89ab46d2 100644 --- a/vrcosc-magicchatbox/MagicChatbox.csproj +++ b/vrcosc-magicchatbox/MagicChatbox.csproj @@ -2,7 +2,7 @@ WinExe - 0.8.875 + 0.8.878 net6.0-windows10.0.22000.0 vrcosc_magicchatbox enable