Skip to content

Commit

Permalink
Fix bugs related to the settings and logger
Browse files Browse the repository at this point in the history
  • Loading branch information
gnh1201 committed Nov 3, 2024
1 parent 020f99c commit 3e78e0b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 57 deletions.
12 changes: 4 additions & 8 deletions WelsonJS.Toolkit/WelsonJS.Service/FileEventMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -86,11 +87,6 @@ public FileEventMonitor(ServiceBase parent, string workingDirectory)
ConnectToClamAv().Start();
}

public void SetLogger(ILogger _logger)
{
logger = _logger;
}

public void Start()
{
try
Expand Down
16 changes: 6 additions & 10 deletions WelsonJS.Toolkit/WelsonJS.Service/HeartbeatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand All @@ -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)
Expand Down
20 changes: 15 additions & 5 deletions WelsonJS.Toolkit/WelsonJS.Service/Program.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
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;

/// <summary>
/// 해당 애플리케이션의 주 진입점입니다.
/// </summary>
///
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)");
Console.WriteLine("https://github.com/gnh1201/welsonjs");
Console.WriteLine();
Console.WriteLine("Service is running...");

ServiceMain svc = new ServiceMain(args);
ServiceMain svc = new ServiceMain(args, logger);
svc.TestStartupAndStop();
}
else
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new ServiceMain(args)
new ServiceMain(args, logger)
};
ServiceBase.Run(ServicesToRun);
}
Expand Down
14 changes: 5 additions & 9 deletions WelsonJS.Toolkit/WelsonJS.Service/ScreenMatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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))
Expand Down
50 changes: 25 additions & 25 deletions WelsonJS.Toolkit/WelsonJS.Service/ServiceMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using WelsonJS.Service.Logging;
using Grpc.Core.Logging;

namespace WelsonJS.Service
{
public partial class ServiceMain : ServiceBase
{
private readonly static string applicationName = "WelsonJS";
private static List<Timer> timers;
private Microsoft.Extensions.Logging.ILogger logger;
private ILogger logger;
private string workingDirectory;
private string scriptName;
private string scriptFilePath;
Expand All @@ -56,25 +55,21 @@ 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")]
private static extern int GetSystemMetrics(int nIndex);

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);
Expand Down Expand Up @@ -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
Expand All @@ -144,7 +139,7 @@ public ServiceMain(string[] args)
}

// read configrations from settings.ini
if (settingsHandler != null)
if (settingsFileHandler != null)
{
string[] configNames = new string[]
{
Expand All @@ -156,7 +151,7 @@ public ServiceMain(string[] args)
{
try
{
if ("true" == GetSettingsHandler().Read(configName, "Service"))
if ("true" == ReadSettingsValue(configName))
{
switch (configName)
{
Expand Down Expand Up @@ -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);
}
Expand All @@ -213,7 +207,7 @@ public ServiceMain(string[] args)

// check this session is the user interactive mode
if (Environment.UserInteractive) {
this.OnUserInteractiveEnvironment();
OnUserInteractiveEnvironment();
}
else
{
Expand All @@ -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()
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit 3e78e0b

Please sign in to comment.