From 9d259a27df8d3826d1c43af21064048a524d144c Mon Sep 17 00:00:00 2001 From: Sparronator9999 <86388887+Sparronator9999@users.noreply.github.com> Date: Sat, 7 Sep 2024 16:37:49 +1000 Subject: [PATCH] (hopefully) Fix exception logging for YAMDCC service While developing the keyboard backlight feature, the service seemed to hang instead of crash without logging the exception --- YAMDCC.Service/Program.cs | 19 ++++++++++++++++++- YAMDCC.Service/svcFanControl.cs | 20 ++++---------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/YAMDCC.Service/Program.cs b/YAMDCC.Service/Program.cs index ed1337c..df6e5b8 100644 --- a/YAMDCC.Service/Program.cs +++ b/YAMDCC.Service/Program.cs @@ -14,18 +14,35 @@ // You should have received a copy of the GNU General Public License along with // YAMDCC. If not, see . +using System; using System.ServiceProcess; +using YAMDCC.Logs; namespace YAMDCC.Service { internal static class Program { + /// + /// The instance to write logs to. + /// + private static readonly Logger Log = new Logger + { + ConsoleLogLevel = LogLevel.None, + FileLogLevel = LogLevel.Debug, + }; + /// /// The main entry point for the application. /// private static void Main() { - ServiceBase.Run(new svcFanControl()); + AppDomain.CurrentDomain.UnhandledException += LogUnhandledException; + ServiceBase.Run(new svcFanControl(Log)); + } + + private static void LogUnhandledException(object sender, UnhandledExceptionEventArgs e) + { + Log.Fatal(Strings.GetString("svcException"), e.ExceptionObject); } } } diff --git a/YAMDCC.Service/svcFanControl.cs b/YAMDCC.Service/svcFanControl.cs index ad46ff8..4daca64 100644 --- a/YAMDCC.Service/svcFanControl.cs +++ b/YAMDCC.Service/svcFanControl.cs @@ -49,14 +49,7 @@ internal sealed partial class svcFanControl : ServiceBase /// private readonly NamedPipeServer IPCServer; - /// - /// The instance to write logs to. - /// - private static readonly Logger Log = new Logger - { - ConsoleLogLevel = LogLevel.None, - FileLogLevel = LogLevel.Debug, - }; + private readonly Logger Log; private readonly EC _EC; #endregion @@ -65,11 +58,11 @@ internal sealed partial class svcFanControl : ServiceBase /// Initialises a new instance of the class. /// /// The instance to write logs to. - public svcFanControl() + public svcFanControl(Logger logger) { InitializeComponent(); - AppDomain.CurrentDomain.UnhandledException += LogUnhandledException; + Log = logger; _EC = new EC(); PipeSecurity security = new PipeSecurity(); @@ -357,7 +350,7 @@ private void ApplySettings() /// The expected number of arguments. Must be zero or positive. /// The parsed arguments. Will be empty if parsing fails. /// - private static bool ParseArgs(string args_in, int expected_args, out int[] args_out) + private bool ParseArgs(string args_in, int expected_args, out int[] args_out) { args_out = new int[expected_args]; @@ -648,10 +641,5 @@ private int SetKeyLightBright(string name, string args) } return 2; } - - private static void LogUnhandledException(object sender, UnhandledExceptionEventArgs e) - { - Log.Fatal(Strings.GetString("svcException"), e.ExceptionObject); - } } }