From 3e78e0b5d20119eec1cf2f0e39ec1d7f97479032 Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Mon, 4 Nov 2024 02:04:02 +0900 Subject: [PATCH] Fix bugs related to the settings and logger --- .../WelsonJS.Service/FileEventMonitor.cs | 12 ++--- .../WelsonJS.Service/HeartbeatClient.cs | 16 +++--- WelsonJS.Toolkit/WelsonJS.Service/Program.cs | 20 ++++++-- .../WelsonJS.Service/ScreenMatch.cs | 14 ++---- .../WelsonJS.Service/ServiceMain.cs | 50 +++++++++---------- 5 files changed, 55 insertions(+), 57 deletions(-) diff --git a/WelsonJS.Toolkit/WelsonJS.Service/FileEventMonitor.cs b/WelsonJS.Toolkit/WelsonJS.Service/FileEventMonitor.cs index 070950b1..3c36d2a3 100644 --- a/WelsonJS.Toolkit/WelsonJS.Service/FileEventMonitor.cs +++ b/WelsonJS.Toolkit/WelsonJS.Service/FileEventMonitor.cs @@ -70,13 +70,14 @@ private enum RegistryEvent: int private string clamAvConenctionString; private IClamAvClient clamAvClient; - public FileEventMonitor(ServiceBase parent, string workingDirectory) + public FileEventMonitor(ServiceBase _parent, string workingDirectory, ILogger _logger) { - this.parent = (ServiceMain)parent; + parent = (ServiceMain)_parent; + logger = _logger; try { - clamAvConenctionString = this.parent.GetSettingsHandler().Read("CLAMAV_HOST", "Service"); + clamAvConenctionString = parent.ReadSettingsValue("CLAMAV_HOST"); } catch (Exception ex) { @@ -86,11 +87,6 @@ public FileEventMonitor(ServiceBase parent, string workingDirectory) ConnectToClamAv().Start(); } - public void SetLogger(ILogger _logger) - { - logger = _logger; - } - public void Start() { try diff --git a/WelsonJS.Toolkit/WelsonJS.Service/HeartbeatClient.cs b/WelsonJS.Toolkit/WelsonJS.Service/HeartbeatClient.cs index 8eb933df..4bb7fa37 100644 --- a/WelsonJS.Toolkit/WelsonJS.Service/HeartbeatClient.cs +++ b/WelsonJS.Toolkit/WelsonJS.Service/HeartbeatClient.cs @@ -19,19 +19,20 @@ public class HeartbeatClient private ILogger logger; private readonly GrpcChannel _channel; private int HeartbeatInterval; - private ServiceMain _parent; + private ServiceMain parent; private string clientId; private string serverAddress; - public HeartbeatClient(ServiceBase parent) + public HeartbeatClient(ServiceBase _parent, ILogger _logger) { - _parent = (ServiceMain)parent; + parent = (ServiceMain)_parent; + logger = _logger; - HeartbeatInterval = int.Parse(_parent.GetSettingsHandler().Read("HEARTBEAT_INTERVAL", "Service") ?? "2000"); + HeartbeatInterval = int.Parse(parent.ReadSettingsValue("HEARTBEAT_INTERVAL") ?? "2000"); try { - serverAddress = _parent.GetSettingsHandler().Read("GRPC_HOST", "Service"); + serverAddress = parent.ReadSettingsValue("GRPC_HOST"); if (String.IsNullOrEmpty(serverAddress)) { throw new Exception("The server address could not be empty."); @@ -56,11 +57,6 @@ public HeartbeatClient(ServiceBase parent) logger.LogInformation($"Use the client ID: {clientId}"); } - public void SetLogger(ILogger _logger) - { - logger = _logger; - } - public async Task StartHeartbeatAsync() { while (true) diff --git a/WelsonJS.Toolkit/WelsonJS.Service/Program.cs b/WelsonJS.Toolkit/WelsonJS.Service/Program.cs index 8cd105fc..59090424 100644 --- a/WelsonJS.Toolkit/WelsonJS.Service/Program.cs +++ b/WelsonJS.Toolkit/WelsonJS.Service/Program.cs @@ -1,17 +1,27 @@ -using System; -using System.Collections.Generic; +using Microsoft.Extensions.Logging; +using System; +using System.IO; using System.ServiceProcess; -using System.Text; +using WelsonJS.Service.Logging; namespace WelsonJS.Service { internal static class Program { + private static ILogger logger; + /// /// 해당 애플리케이션의 주 진입점입니다. /// + /// static void Main(string[] args) { + // create the logger + ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole()); + factory.AddDirectory(Path.GetTempPath()); + logger = factory.CreateLogger("welsonjs"); + + // create the service if (Environment.UserInteractive) { Console.WriteLine("WelsonJS Service Application (User Interactive Mode)"); @@ -19,7 +29,7 @@ static void Main(string[] args) Console.WriteLine(); Console.WriteLine("Service is running..."); - ServiceMain svc = new ServiceMain(args); + ServiceMain svc = new ServiceMain(args, logger); svc.TestStartupAndStop(); } else @@ -27,7 +37,7 @@ static void Main(string[] args) ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { - new ServiceMain(args) + new ServiceMain(args, logger) }; ServiceBase.Run(ServicesToRun); } diff --git a/WelsonJS.Toolkit/WelsonJS.Service/ScreenMatch.cs b/WelsonJS.Toolkit/WelsonJS.Service/ScreenMatch.cs index 051e12da..6e6aff78 100644 --- a/WelsonJS.Toolkit/WelsonJS.Service/ScreenMatch.cs +++ b/WelsonJS.Toolkit/WelsonJS.Service/ScreenMatch.cs @@ -159,9 +159,10 @@ public SampleInfo(string fileName, uint crc32) } } - public ScreenMatch(ServiceBase parent, string workingDirectory) + public ScreenMatch(ServiceBase _parent, string workingDirectory, ILogger _logger) { - this.parent = (ServiceMain)parent; + parent = (ServiceMain)_parent; + logger = _logger; SetBusy(false); @@ -193,8 +194,8 @@ public ScreenMatch(ServiceBase parent, string workingDirectory) string screen_time_params; try { - screen_time_mode = this.parent.GetSettingsHandler().Read("SCREEN_TIME_MODE", "Service"); - screen_time_params = this.parent.GetSettingsHandler().Read("SCREEN_TIME_PARAMS", "Service"); + screen_time_mode = parent.ReadSettingsValue("SCREEN_TIME_MODE"); + screen_time_params = parent.ReadSettingsValue("SCREEN_TIME_PARAMS"); } catch (Exception ex) { @@ -332,11 +333,6 @@ public ScreenMatch(ServiceBase parent, string workingDirectory) LoadTemplateImages(); } - public void SetLogger(ILogger _logger) - { - logger = _logger; - } - public void SetMode(string mode) { if (!String.IsNullOrEmpty(mode)) diff --git a/WelsonJS.Toolkit/WelsonJS.Service/ServiceMain.cs b/WelsonJS.Toolkit/WelsonJS.Service/ServiceMain.cs index e5d436c1..5c45fe12 100644 --- a/WelsonJS.Toolkit/WelsonJS.Service/ServiceMain.cs +++ b/WelsonJS.Toolkit/WelsonJS.Service/ServiceMain.cs @@ -36,7 +36,6 @@ using System.Threading.Tasks; using Microsoft.Extensions.Logging; using WelsonJS.Service.Logging; -using Grpc.Core.Logging; namespace WelsonJS.Service { @@ -44,7 +43,7 @@ public partial class ServiceMain : ServiceBase { private readonly static string applicationName = "WelsonJS"; private static List timers; - private Microsoft.Extensions.Logging.ILogger logger; + private ILogger logger; private string workingDirectory; private string scriptName; private string scriptFilePath; @@ -56,7 +55,7 @@ public partial class ServiceMain : ServiceBase private bool disabledFileMonitor = false; private ScreenMatch screenMatcher; private FileEventMonitor fileEventMonitor; - private IniFile settingsHandler; + private IniFile settingsFileHandler; private UserVariables userVariablesHandler; [DllImport("user32.dll")] @@ -64,17 +63,13 @@ public partial class ServiceMain : ServiceBase private static int SM_REMOTESESSION = 0x1000; - public ServiceMain(string[] args) + public ServiceMain(string[] _args, ILogger _logger) { InitializeComponent(); - // set service arguments - this.args = args; - - // set the logger - ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole()); - factory.AddDirectory(Path.GetTempPath()); - logger = factory.CreateLogger(applicationName.ToLower()); + // set arguments and logger + args = _args; + logger = _logger; // mapping arguments to each variables var arguments = ParseArguments(this.args); @@ -131,11 +126,11 @@ public ServiceMain(string[] args) { try { - settingsHandler = new IniFile(settingsFilePath); + settingsFileHandler = new IniFile(settingsFilePath); } - catch (Exception) + catch (Exception ex) { - settingsHandler = null; + logger.LogWarning(ex.Message); } } else @@ -144,7 +139,7 @@ public ServiceMain(string[] args) } // read configrations from settings.ini - if (settingsHandler != null) + if (settingsFileHandler != null) { string[] configNames = new string[] { @@ -156,7 +151,7 @@ public ServiceMain(string[] args) { try { - if ("true" == GetSettingsHandler().Read(configName, "Service")) + if ("true" == ReadSettingsValue(configName)) { switch (configName) { @@ -197,8 +192,7 @@ public ServiceMain(string[] args) // start the heartbeat if (!disabledHeartbeat) { - HeartbeatClient heartbeatClient = new HeartbeatClient(this); - heartbeatClient.SetLogger(logger); + HeartbeatClient heartbeatClient = new HeartbeatClient(this, logger); Task.Run(heartbeatClient.StartHeartbeatAsync); Task.Run(heartbeatClient.StartEventListenerAsync); } @@ -213,7 +207,7 @@ public ServiceMain(string[] args) // check this session is the user interactive mode if (Environment.UserInteractive) { - this.OnUserInteractiveEnvironment(); + OnUserInteractiveEnvironment(); } else { @@ -223,9 +217,17 @@ public ServiceMain(string[] args) logger.LogInformation(applicationName + " Service Loaded"); } - public IniFile GetSettingsHandler() + public string ReadSettingsValue(string key, string defaultValue = null) { - return settingsHandler; + if (settingsFileHandler != null) + { + return settingsFileHandler.Read(key, "Service") ?? defaultValue; + } + else + { + logger.LogWarning("Unable to read the value. It seems that settings.ini is not configured correctly."); + return defaultValue; + } } public UserVariables GetUserVariablesHandler() @@ -302,8 +304,7 @@ protected override void OnStart(string[] args) // Trace a Sysmon file events (If Sysinternals Sysmon installed) if (!disabledFileMonitor) { - fileEventMonitor = new FileEventMonitor(this, workingDirectory); - fileEventMonitor.SetLogger(logger); + fileEventMonitor = new FileEventMonitor(this, workingDirectory, logger); fileEventMonitor.Start(); logger.LogInformation("File Event Monitor Started"); @@ -354,8 +355,7 @@ private void OnUserInteractiveEnvironment() // set screen timer if (!disabledScreenTime) { - screenMatcher = new ScreenMatch(this, workingDirectory); - screenMatcher.SetLogger(logger); + screenMatcher = new ScreenMatch(this, workingDirectory, logger); Timer screenTimer = new Timer {