diff --git a/WelsonJS.Toolkit/WelsonJS.Service/FileEventMonitor.cs b/WelsonJS.Toolkit/WelsonJS.Service/FileEventMonitor.cs
index 070950b..3c36d2a 100644
--- a/WelsonJS.Toolkit/WelsonJS.Service/FileEventMonitor.cs
+++ b/WelsonJS.Toolkit/WelsonJS.Service/FileEventMonitor.cs
@@ -70,13 +70,14 @@ namespace WelsonJS.Service
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 @@ namespace WelsonJS.Service
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 8eb933d..4bb7fa3 100644
--- a/WelsonJS.Toolkit/WelsonJS.Service/HeartbeatClient.cs
+++ b/WelsonJS.Toolkit/WelsonJS.Service/HeartbeatClient.cs
@@ -19,19 +19,20 @@ namespace WelsonJS.Service
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 @@ namespace WelsonJS.Service
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 8cd105f..5909042 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 @@ namespace WelsonJS.Service
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 @@ namespace WelsonJS.Service
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 051e12d..6e6aff7 100644
--- a/WelsonJS.Toolkit/WelsonJS.Service/ScreenMatch.cs
+++ b/WelsonJS.Toolkit/WelsonJS.Service/ScreenMatch.cs
@@ -159,9 +159,10 @@ public class ScreenMatch
}
}
- 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 class ScreenMatch
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 class ScreenMatch
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 e5d436c..5c45fe1 100644
--- a/WelsonJS.Toolkit/WelsonJS.Service/ServiceMain.cs
+++ b/WelsonJS.Toolkit/WelsonJS.Service/ServiceMain.cs
@@ -36,7 +36,6 @@ using System.Collections;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using WelsonJS.Service.Logging;
-using Grpc.Core.Logging;
namespace WelsonJS.Service
{
@@ -44,7 +43,7 @@ namespace WelsonJS.Service
{
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 @@ namespace WelsonJS.Service
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 @@ namespace WelsonJS.Service
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 @@ namespace WelsonJS.Service
{
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 @@ namespace WelsonJS.Service
}
// read configrations from settings.ini
- if (settingsHandler != null)
+ if (settingsFileHandler != null)
{
string[] configNames = new string[]
{
@@ -156,7 +151,7 @@ namespace WelsonJS.Service
{
try
{
- if ("true" == GetSettingsHandler().Read(configName, "Service"))
+ if ("true" == ReadSettingsValue(configName))
{
switch (configName)
{
@@ -197,8 +192,7 @@ namespace WelsonJS.Service
// 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 @@ namespace WelsonJS.Service
// check this session is the user interactive mode
if (Environment.UserInteractive) {
- this.OnUserInteractiveEnvironment();
+ OnUserInteractiveEnvironment();
}
else
{
@@ -223,9 +217,17 @@ namespace WelsonJS.Service
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 @@ namespace WelsonJS.Service
// 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 @@ namespace WelsonJS.Service
// set screen timer
if (!disabledScreenTime)
{
- screenMatcher = new ScreenMatch(this, workingDirectory);
- screenMatcher.SetLogger(logger);
+ screenMatcher = new ScreenMatch(this, workingDirectory, logger);
Timer screenTimer = new Timer
{