Skip to content

Commit

Permalink
Refactor logging and update project version
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
BoiHanny committed May 18, 2024
1 parent 9a13ad3 commit 0ba44d3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
43 changes: 32 additions & 11 deletions vrcosc-magicchatbox/Classes/DataAndSecurity/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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}");
}
}
}
}
2 changes: 1 addition & 1 deletion vrcosc-magicchatbox/MagicChatbox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<Version>0.8.875</Version>
<Version>0.8.878</Version>
<TargetFramework>net6.0-windows10.0.22000.0</TargetFramework>
<RootNamespace>vrcosc_magicchatbox</RootNamespace>
<Nullable>enable</Nullable>
Expand Down

0 comments on commit 0ba44d3

Please sign in to comment.