From 99c0c628dc28c611de69d0d808f384d2644de229 Mon Sep 17 00:00:00 2001 From: djbadders <34887832+djbadders@users.noreply.github.com> Date: Mon, 11 Nov 2019 23:36:18 +0000 Subject: [PATCH] Added unhandled exception logging to Core and Monitor --- Core/Main/PTMagic.cs | 6 +- Monitor/Monitor.csproj | 1 + Monitor/Program.cs | 155 ++++++++++++++++++++++++++++------------- Monitor/nlog.config | 28 ++++++++ PTMagic/Program.cs | 31 +++++++-- 5 files changed, 163 insertions(+), 58 deletions(-) create mode 100644 Monitor/nlog.config diff --git a/Core/Main/PTMagic.cs b/Core/Main/PTMagic.cs index be101bf..8c3598e 100644 --- a/Core/Main/PTMagic.cs +++ b/Core/Main/PTMagic.cs @@ -3,14 +3,10 @@ using System.Collections.Generic; using System.Threading; using System.IO; using System.Linq; -using System.Reflection; -using Core.Main; using Core.Helper; using Core.Main.DataObjects.PTMagicData; using Core.MarketAnalyzer; using Core.ProfitTrailer; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; namespace Core.Main @@ -537,7 +533,7 @@ namespace Core.Main public bool StartProcess() { bool result = true; - + this.Log.DoLogInfo(""); this.Log.DoLogInfo(" ██████╗ ████████╗ ███╗ ███╗ █████╗ ██████╗ ██╗ ██████╗"); this.Log.DoLogInfo(" ██╔══██╗╚══██╔══╝ ████╗ ████║██╔══██╗██╔════╝ ██║██╔════╝"); diff --git a/Monitor/Monitor.csproj b/Monitor/Monitor.csproj index 4aa31e8..b118416 100644 --- a/Monitor/Monitor.csproj +++ b/Monitor/Monitor.csproj @@ -20,6 +20,7 @@ Always + diff --git a/Monitor/Program.cs b/Monitor/Program.cs index 6e6768f..a9a8294 100644 --- a/Monitor/Program.cs +++ b/Monitor/Program.cs @@ -1,35 +1,49 @@ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Net; -using System.Threading.Tasks; -using Microsoft.AspNetCore; +using System.Security.Permissions; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Logging; using Core.Main; +using Core.Helper; +using Microsoft.Extensions.DependencyInjection; -namespace Monitor { - public class Program { - public static void Main(string[] args) { - Console.WriteLine("##########################################################"); - Console.WriteLine("#********************************************************#"); - Console.WriteLine("INFO: Starting PT Magic Monitor..."); - Console.WriteLine("INFO: Beginning startup checks..."); +namespace Monitor +{ + public class Program + { + + // Create a logger + private static LogHelper _log = ServiceHelper.BuildLoggerService().GetRequiredService(); + + // Main entry point + [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)] + public static void Main(string[] args) + { + // Register a global exception handler + AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(GlobalUnhandledExceptionHandler); + + // Start + WriteConsoleLogLine("##########################################################"); + WriteConsoleLogLine("#********************************************************#"); + WriteConsoleLogLine("INFO: Starting PT Magic Monitor..."); + WriteConsoleLogLine("INFO: Beginning startup checks..."); string monitorBasePath = Directory.GetCurrentDirectory(); - if (!System.IO.File.Exists(monitorBasePath + Path.DirectorySeparatorChar + "appsettings.json")) { + if (!System.IO.File.Exists(monitorBasePath + Path.DirectorySeparatorChar + "appsettings.json")) + { monitorBasePath += Path.DirectorySeparatorChar + "Monitor"; } // Startup checks string appsettingsJson = monitorBasePath + Path.DirectorySeparatorChar + "appsettings.json"; - if (!File.Exists(appsettingsJson)) { - Console.WriteLine("ERROR: appsettings.json not found: '" + appsettingsJson + "'. Please check if the file exists. If not, review the PT Magic setup steps listed on the wiki!"); + if (!File.Exists(appsettingsJson)) + { + WriteConsoleLogLine("ERROR: appsettings.json not found: '" + appsettingsJson + "'. Please check if the file exists. If not, review the PT Magic setup steps listed on the wiki!"); if (!Console.IsInputRedirected) Console.ReadKey(); - } else { - Console.WriteLine("INFO: appsettings.json found in " + monitorBasePath); + } + else + { + WriteConsoleLogLine("INFO: appsettings.json found in " + monitorBasePath); IConfiguration config = new ConfigurationBuilder() .SetBasePath(monitorBasePath) @@ -38,61 +52,80 @@ namespace Monitor { string ptMagicBasePath = config.GetValue("PTMagicBasePath"); - if (!ptMagicBasePath.EndsWith(Path.DirectorySeparatorChar)) { + if (!ptMagicBasePath.EndsWith(Path.DirectorySeparatorChar)) + { ptMagicBasePath += Path.DirectorySeparatorChar; } // More startup checks // Check if PT Magic directoy is correctly configured - if (!Directory.Exists(ptMagicBasePath)) { - Console.WriteLine("ERROR: PT Magic directory not found: '" + ptMagicBasePath + "'. Please check your setting for 'PTMagicBasePath' in 'Monitor/appsettings.json'"); + if (!Directory.Exists(ptMagicBasePath)) + { + WriteConsoleLogLine("ERROR: PT Magic directory not found: '" + ptMagicBasePath + "'. Please check your setting for 'PTMagicBasePath' in 'Monitor/appsettings.json'"); if (!Console.IsInputRedirected) Console.ReadKey(); - } else { - Console.WriteLine("INFO: PT Magic directory found at " + ptMagicBasePath); + } + else + { + WriteConsoleLogLine("INFO: PT Magic directory found at " + ptMagicBasePath); // Check if PT Magic settings file exists string settingsGeneralJson = ptMagicBasePath + "settings.general.json"; - if (!File.Exists(settingsGeneralJson)) { - Console.WriteLine("ERROR: PT Magic settings not found: '" + settingsGeneralJson + "'. Please check if you setup PT Magic correctly!"); + if (!File.Exists(settingsGeneralJson)) + { + WriteConsoleLogLine("ERROR: PT Magic settings not found: '" + settingsGeneralJson + "'. Please check if you setup PT Magic correctly!"); if (!Console.IsInputRedirected) Console.ReadKey(); - } else { - Console.WriteLine("INFO: settings.general.json found at " + settingsGeneralJson); + } + else + { + WriteConsoleLogLine("INFO: settings.general.json found at " + settingsGeneralJson); // Check if PT Magic settings file exists string lastRuntimeSummaryJson = ptMagicBasePath + Constants.PTMagicPathData + Path.DirectorySeparatorChar + "LastRuntimeSummary.json"; - if (!File.Exists(lastRuntimeSummaryJson)) { - Console.WriteLine("ERROR: PT Magic runtime summary not found: '" + lastRuntimeSummaryJson + "'. Please wait for PT Magic to complete its first run!"); + if (!File.Exists(lastRuntimeSummaryJson)) + { + WriteConsoleLogLine("ERROR: PT Magic runtime summary not found: '" + lastRuntimeSummaryJson + "'. Please wait for PT Magic to complete its first run!"); if (!Console.IsInputRedirected) Console.ReadKey(); - } else { - Console.WriteLine("INFO: LastRuntimeSummary.json found at " + lastRuntimeSummaryJson); + } + else + { + WriteConsoleLogLine("INFO: LastRuntimeSummary.json found at " + lastRuntimeSummaryJson); PTMagicConfiguration ptMagicConfiguration = null; - try { + try + { ptMagicConfiguration = new PTMagicConfiguration(ptMagicBasePath); - } catch (Exception ex) { + } + catch (Exception ex) + { throw ex; } string wwwrootPath = monitorBasePath + Path.DirectorySeparatorChar + "wwwroot"; - if (!Directory.Exists(wwwrootPath)) { - Console.WriteLine("ERROR: wwwroot directory not found: '" + wwwrootPath + "'. Did you copy all files as instructed on the wiki?"); + if (!Directory.Exists(wwwrootPath)) + { + WriteConsoleLogLine("ERROR: wwwroot directory not found: '" + wwwrootPath + "'. Did you copy all files as instructed on the wiki?"); if (!Console.IsInputRedirected) Console.ReadKey(); - } else { - Console.WriteLine("INFO: wwwroot directory found at " + wwwrootPath); + } + else + { + WriteConsoleLogLine("INFO: wwwroot directory found at " + wwwrootPath); string assetsPath = wwwrootPath + Path.DirectorySeparatorChar + "assets"; - if (!Directory.Exists(assetsPath)) { - Console.WriteLine("ERROR: assets directory not found: '" + assetsPath + "'. Did you copy all files as instructed on the wiki?"); + if (!Directory.Exists(assetsPath)) + { + WriteConsoleLogLine("ERROR: assets directory not found: '" + assetsPath + "'. Did you copy all files as instructed on the wiki?"); if (!Console.IsInputRedirected) Console.ReadKey(); - } else { - Console.WriteLine("INFO: assets directory found at " + assetsPath); - Console.WriteLine("INFO: ALL CHECKS COMPLETED - ATTEMPTING TO START WEBSERVER..."); - Console.WriteLine("#********************************************************#"); - Console.WriteLine(""); - Console.WriteLine("DO NOT CLOSE THIS WINDOW! THIS IS THE WEBSERVER FOR YOUR MONITOR!"); - Console.WriteLine(""); - Console.WriteLine("##########################################################"); - Console.WriteLine(""); + } + else + { + WriteConsoleLogLine("INFO: assets directory found at " + assetsPath); + WriteConsoleLogLine("INFO: ALL CHECKS COMPLETED - ATTEMPTING TO START WEBSERVER..."); + WriteConsoleLogLine("#********************************************************#"); + WriteConsoleLogLine(""); + WriteConsoleLogLine("DO NOT CLOSE THIS WINDOW! THIS IS THE WEBSERVER FOR YOUR MONITOR!"); + WriteConsoleLogLine(""); + WriteConsoleLogLine("##########################################################"); + WriteConsoleLogLine(""); BuildWebHost(args, monitorBasePath, monitorBasePath + Path.DirectorySeparatorChar + "wwwroot", ptMagicConfiguration.GeneralSettings.Monitor.Port).Run(); } @@ -112,5 +145,31 @@ namespace Monitor { .UseContentRoot(contentRoot) .UseWebRoot(webroot) .Build(); + + // Log writer + private static void WriteConsoleLogLine(string line) + { + // Write to console and log + Console.WriteLine(line); + _log.DoLogInfo(line); + } + + // Global unhandled exception handler + private static void GlobalUnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args) + { + Exception e = (Exception)args.ExceptionObject; + + Console.WriteLine("Unhandled exception occurred: " + e.ToString()); + + if (args.IsTerminating) + { + _log.DoLogCritical("Unhandled fatal exception occurred: ", e); + } + else + { + _log.DoLogError("Unhandled fatal exception occurred: " + e.ToString()); + } + } } + } diff --git a/Monitor/nlog.config b/Monitor/nlog.config new file mode 100644 index 0000000..83af132 --- /dev/null +++ b/Monitor/nlog.config @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/PTMagic/Program.cs b/PTMagic/Program.cs index 926dfec..e6ba5ec 100644 --- a/PTMagic/Program.cs +++ b/PTMagic/Program.cs @@ -1,10 +1,8 @@ using System; using System.Threading; -using System.IO; using System.Reflection; -using Core.Main; +using System.Security.Permissions; using Core.Helper; -using Core.Main.DataObjects.PTMagicData; using Microsoft.Extensions.DependencyInjection; [assembly: AssemblyVersion("2.2.10")] @@ -14,10 +12,18 @@ namespace PTMagic { class Program { - static void Main(string[] args) + // Create a logger + private static LogHelper _log = ServiceHelper.BuildLoggerService().GetRequiredService(); + + // Main class entry + [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)] + public static void Main(string[] args) { + // Register a global exception handler + AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(GlobalUnhandledExceptionHandler); + // Init PTMagic - Core.Main.PTMagic ptMagic = new Core.Main.PTMagic(ServiceHelper.BuildLoggerService().GetRequiredService()); + Core.Main.PTMagic ptMagic = new Core.Main.PTMagic(_log); ptMagic.CurrentVersion = Assembly.GetExecutingAssembly().GetName().Version; // Start process @@ -29,5 +35,20 @@ namespace PTMagic Thread.Sleep(10000); } } + + // Global unhandled exception handler + private static void GlobalUnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args) + { + Exception e = (Exception)args.ExceptionObject; + + if (args.IsTerminating) + { + _log.DoLogCritical("Unhandled fatal exception occurred: ", e); + } + else + { + _log.DoLogError("Unhandled fatal exception occurred: " + e.ToString()); + } + } } }