Skip to content

Commit d3a9df7

Browse files
Update to fix Issue #61
- Fixed issue with ServiceName, ServiceDisplayName, and ServiceDescription. These can now be set in the configuration file to override the defaults - Fixed issue with passing a configuration path when installing as a Windows Service - Fixed issue with application-logger Log file
1 parent 660f21b commit d3a9df7

6 files changed

Lines changed: 189 additions & 122 deletions

File tree

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"profiles": {
33
"MTConnect.NET-Agent": {
4-
"commandName": "Project"
4+
"commandName": "Project",
5+
"commandLineArgs": "debug"
56
}
67
}
78
}

agent/MTConnect.NET-Applications-Agents/MTConnectAgentApplication.cs

Lines changed: 137 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public class MTConnectAgentApplication : IMTConnectAgentApplication
3030
private const string DefaultServiceDisplayName = "MTConnect.NET Agent";
3131
private const string DefaultServiceDescription = "MTConnect Agent to provide access to device information using the MTConnect Standard";
3232

33-
protected readonly Logger _applicationLogger = LogManager.GetLogger("application");
34-
protected readonly Logger _agentLogger = LogManager.GetLogger("agent");
33+
protected readonly Logger _applicationLogger = LogManager.GetLogger("application-logger");
34+
protected readonly Logger _agentLogger = LogManager.GetLogger("agent-logger");
3535
protected readonly Logger _agentMetricLogger = LogManager.GetLogger("agent-metrics");
3636
protected readonly Logger _agentValidationLogger = LogManager.GetLogger("agent-validation");
3737
protected readonly Logger _moduleLogger = LogManager.GetLogger("module");
@@ -68,9 +68,13 @@ public class MTConnectAgentApplication : IMTConnectAgentApplication
6868

6969
public MTConnectAgentApplication(IAgentApplicationConfiguration agentConfiguration = null)
7070
{
71-
ServiceName = DefaultServiceName;
72-
ServiceDisplayName = DefaultServiceDisplayName;
73-
ServiceDescription = DefaultServiceDescription;
71+
if (agentConfiguration != null)
72+
{
73+
ServiceName = !string.IsNullOrEmpty(agentConfiguration.ServiceName) ? agentConfiguration.ServiceName : DefaultServiceName;
74+
ServiceDisplayName = !string.IsNullOrEmpty(agentConfiguration.ServiceDisplayName) ? agentConfiguration.ServiceDisplayName : DefaultServiceDisplayName;
75+
ServiceDescription = !string.IsNullOrEmpty(agentConfiguration.ServiceDescription) ? agentConfiguration.ServiceDescription : DefaultServiceDescription;
76+
}
77+
7478
_initialAgentConfiguration = agentConfiguration;
7579
}
7680

@@ -146,160 +150,175 @@ public void Run(string[] args, bool isBlocking = false)
146150
if (configuration != null)
147151
{
148152
// Set Service Name
149-
if (!string.IsNullOrEmpty(configuration.ServiceName)) serviceDisplayName = configuration.ServiceName;
153+
if (!string.IsNullOrEmpty(configuration.ServiceName)) serviceName = configuration.ServiceName;
154+
if (!string.IsNullOrEmpty(configuration.ServiceDisplayName)) serviceDisplayName = configuration.ServiceDisplayName;
155+
if (!string.IsNullOrEmpty(configuration.ServiceDescription)) serviceDescription = configuration.ServiceDescription;
150156

151157
// Set Service Auto Start
152158
serviceStart = configuration.ServiceAutoStart;
153159
}
154-
else
155-
{
156-
_applicationLogger.Warn("Error Reading Configuration File. Default Configuration is loaded.");
157160

158-
configuration = new AgentApplicationConfiguration();
159-
}
160161

161-
// Declare a new Service (to use Service commands)
162-
Service service = null;
163-
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
162+
if (configuration != null)
164163
{
165-
service = new Service(this, serviceName, serviceDisplayName, serviceDescription, serviceStart);
166-
}
164+
// Declare a new Service (to use Service commands)
165+
Service service = null;
166+
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
167+
{
168+
service = new Service(this, serviceName, serviceDisplayName, serviceDescription, serviceStart);
169+
service.LogInformationReceived += ServiceLogInformationReceived;
170+
}
167171

168-
switch (command)
169-
{
170-
case "run":
171-
_verboseLogging = false;
172-
StartAgent(configuration, _verboseLogging);
172+
switch (command)
173+
{
174+
case "run":
175+
_verboseLogging = false;
176+
StartAgent(configuration, _verboseLogging);
173177

174-
if (isBlocking) while (true) Thread.Sleep(100); // Block (exit console by 'Ctrl + C')
175-
else break;
178+
if (isBlocking) while (true) Thread.Sleep(100); // Block (exit console by 'Ctrl + C')
179+
else break;
176180

177-
case "run-service":
181+
case "run-service":
178182

179-
_verboseLogging = true;
180-
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
181-
{
182-
ServiceBase.Run(service);
183-
}
184-
else _applicationLogger.Info($"'Run-Service' Command is not supported on this Operating System");
183+
_verboseLogging = true;
184+
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
185+
{
186+
ServiceBase.Run(service);
187+
}
188+
else _applicationLogger.Info($"'Run-Service' Command is not supported on this Operating System");
185189

186-
break;
190+
break;
187191

188-
case "debug":
192+
case "debug":
189193

190-
if (LogManager.Configuration != null)
191-
{
192-
var consoleTarget = LogManager.Configuration.FindTargetByName("logConsole");
193-
if (consoleTarget != null)
194+
if (LogManager.Configuration != null)
194195
{
195-
LogManager.Configuration.AddRule(new LoggingRule("*", LogLevel.Debug, consoleTarget));
196+
var consoleTarget = LogManager.Configuration.FindTargetByName("logConsole");
197+
if (consoleTarget != null)
198+
{
199+
LogManager.Configuration.AddRule(new LoggingRule("*", LogLevel.Debug, consoleTarget));
200+
}
196201
}
197-
}
198202

199-
StartAgent(configuration, true);
203+
StartAgent(configuration, true);
200204

201-
if (isBlocking) while (true) Thread.Sleep(100); // Block (exit console by 'Ctrl + C')
202-
else break;
205+
if (isBlocking) while (true) Thread.Sleep(100); // Block (exit console by 'Ctrl + C')
206+
else break;
203207

204-
case "trace":
208+
case "trace":
205209

206-
if (LogManager.Configuration != null)
207-
{
208-
var consoleTarget = LogManager.Configuration.FindTargetByName("logConsole");
209-
if (consoleTarget != null)
210+
if (LogManager.Configuration != null)
210211
{
211-
LogManager.Configuration.AddRule(new LoggingRule("*", LogLevel.Trace, consoleTarget));
212+
var consoleTarget = LogManager.Configuration.FindTargetByName("logConsole");
213+
if (consoleTarget != null)
214+
{
215+
LogManager.Configuration.AddRule(new LoggingRule("*", LogLevel.Trace, consoleTarget));
216+
}
212217
}
213-
}
214218

215-
StartAgent(configuration, true);
219+
StartAgent(configuration, true);
216220

217-
if (isBlocking) while (true) Thread.Sleep(100); // Block (exit console by 'Ctrl + C')
218-
else break;
221+
if (isBlocking) while (true) Thread.Sleep(100); // Block (exit console by 'Ctrl + C')
222+
else break;
219223

220-
case "install":
224+
case "install":
221225

222-
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
223-
{
224-
service.StopService();
225-
service.RemoveService();
226-
service.InstallService(configFile);
227-
}
228-
else _applicationLogger.Info($"'Install' Command is not supported on this Operating System");
226+
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
227+
{
228+
service.StopService();
229+
service.RemoveService();
230+
service.InstallService(configFile);
231+
}
232+
else _applicationLogger.Info($"'Install' Command is not supported on this Operating System");
229233

230-
break;
234+
break;
231235

232-
case "install-start":
236+
case "install-start":
233237

234-
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
235-
{
236-
service.StopService();
237-
service.RemoveService();
238-
service.InstallService(configFile);
239-
service.StartService();
240-
}
241-
else _applicationLogger.Info($"'Install-Start' Command is not supported on this Operating System");
238+
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
239+
{
240+
service.StopService();
241+
service.RemoveService();
242+
service.InstallService(configFile);
243+
service.StartService();
244+
}
245+
else _applicationLogger.Info($"'Install-Start' Command is not supported on this Operating System");
242246

243-
break;
247+
break;
244248

245-
case "remove":
249+
case "remove":
246250

247-
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
248-
{
249-
service.StopService();
250-
service.RemoveService();
251-
}
252-
else _applicationLogger.Info($"'Remove' Command is not supported on this Operating System");
251+
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
252+
{
253+
service.StopService();
254+
service.RemoveService();
255+
}
256+
else _applicationLogger.Info($"'Remove' Command is not supported on this Operating System");
253257

254-
break;
258+
break;
255259

256-
case "start":
260+
case "start":
257261

258-
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
259-
{
260-
service.StartService();
261-
}
262-
else _applicationLogger.Info($"'Start' Command is not supported on this Operating System");
262+
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
263+
{
264+
service.StartService();
265+
}
266+
else _applicationLogger.Info($"'Start' Command is not supported on this Operating System");
263267

264-
break;
268+
break;
265269

266-
case "stop":
270+
case "stop":
267271

268-
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
269-
{
270-
service.StopService();
271-
}
272-
else _applicationLogger.Info($"'Stop' Command is not supported on this Operating System");
272+
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
273+
{
274+
service.StopService();
275+
}
276+
else _applicationLogger.Info($"'Stop' Command is not supported on this Operating System");
277+
278+
break;
279+
280+
281+
case "reset":
273282

274-
break;
283+
_applicationLogger.Info("Reset Buffer requested..");
275284

285+
// Clear the Observation Buffer
286+
MTConnectObservationFileBuffer.Reset();
287+
_applicationLogger.Info("Observation Buffer Reset Successfully");
276288

277-
case "reset":
289+
// Clear the Asset Buffer
290+
MTConnectAssetFileBuffer.Reset();
291+
_applicationLogger.Info("Asset Buffer Reset Successfully");
278292

279-
_applicationLogger.Info("Reset Buffer requested..");
293+
// Clear the Index
294+
FileIndex.Reset();
295+
_applicationLogger.Info("Indexes Reset Successfully");
280296

281-
// Clear the Observation Buffer
282-
MTConnectObservationFileBuffer.Reset();
283-
_applicationLogger.Info("Observation Buffer Reset Successfully");
297+
break;
284298

285-
// Clear the Asset Buffer
286-
MTConnectAssetFileBuffer.Reset();
287-
_applicationLogger.Info("Asset Buffer Reset Successfully");
299+
case "help":
300+
PrintHelp();
301+
break;
288302

289-
// Clear the Index
290-
FileIndex.Reset();
291-
_applicationLogger.Info("Indexes Reset Successfully");
303+
default:
304+
_applicationLogger.Info($"'{command}' : Command not recognized : See help for more information");
305+
PrintHelp();
306+
break;
307+
}
308+
}
309+
else
310+
{
311+
var configurationPath = configFile;
312+
if (!Path.IsPathRooted(configurationPath))
313+
{
314+
configurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configurationPath);
315+
}
292316

293-
break;
317+
_applicationLogger.Fatal($"Error Reading Configuration File. Path = {configurationPath}");
294318

295-
case "help":
296-
PrintHelp();
297-
break;
319+
//_applicationLogger.Warn("Error Reading Configuration File. Default Configuration is loaded.");
298320

299-
default:
300-
_applicationLogger.Info($"'{command}' : Command not recognized : See help for more information");
301-
PrintHelp();
302-
break;
321+
//configuration = new AgentApplicationConfiguration();
303322
}
304323
}
305324

@@ -680,9 +699,15 @@ protected virtual void OnPrintHelpArguments() { }
680699

681700
#region "Logging"
682701

702+
private void ServiceLogInformationReceived(object sender, string message)
703+
{
704+
_applicationLogger.Info("Windows Service : " + message);
705+
}
706+
707+
683708
private void ModuleLoaded(object sender, IMTConnectAgentModule module)
684709
{
685-
_applicationLogger.Info($"Module Loaded : " + module.Id);
710+
_applicationLogger.Info("Module Loaded : " + module.Id);
686711
}
687712

688713
private void ModuleLogReceived(object sender, MTConnectLogLevel logLevel, string message, string logId = null)

build/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Reflection;
22

3-
[assembly: AssemblyVersion("6.4.0")]
4-
[assembly: AssemblyFileVersion("6.4.0")]
3+
[assembly: AssemblyVersion("6.4.1")]
4+
[assembly: AssemblyFileVersion("6.4.1")]
55
[assembly: AssemblyCompany("TrakHound Inc.")]
66
[assembly: AssemblyCopyright("Copyright (c) 2024 TrakHound Inc., All Rights Reserved.")]

libraries/MTConnect.NET-Common/Configurations/AgentApplicationConfiguration.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ public class AgentApplicationConfiguration : AgentConfiguration, IAgentApplicati
2929
[JsonPropertyName("serviceName")]
3030
public string ServiceName { get; set; }
3131

32+
/// <summary>
33+
/// Changes the display name of the service. This helps with identification when multiple agents on run as services on the same machine.
34+
/// </summary>
35+
[JsonPropertyName("serviceDisplayName")]
36+
public string ServiceDisplayName { get; set; }
37+
38+
/// <summary>
39+
/// Changes the description of the service. This helps with identification when multiple agents on run as services on the same machine.
40+
/// </summary>
41+
[JsonPropertyName("serviceDisplayName")]
42+
public string ServiceDescription { get; set; }
43+
3244
/// <summary>
3345
/// Sets the Service Start Type. True = Auto | False = Manual
3446
/// </summary>
@@ -73,6 +85,8 @@ public AgentApplicationConfiguration() : base()
7385
{
7486
Devices = null;
7587
ServiceName = null;
88+
ServiceDisplayName = null;
89+
ServiceDescription = null;
7690
ServiceAutoStart = true;
7791
MonitorConfigurationFiles = true;
7892
ConfigurationFileRestartInterval = 2;

libraries/MTConnect.NET-Common/Configurations/IAgentApplicationConfiguration.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ public interface IAgentApplicationConfiguration : IAgentConfiguration
2222
/// </summary>
2323
string ServiceName { get; set; }
2424

25+
/// <summary>
26+
/// Changes the display name of the service. This helps with identification when multiple agents on run as services on the same machine.
27+
/// </summary>
28+
string ServiceDisplayName { get; set; }
29+
30+
/// <summary>
31+
/// Changes the description of the service. This helps with identification when multiple agents on run as services on the same machine.
32+
/// </summary>
33+
string ServiceDescription { get; set; }
34+
2535
/// <summary>
2636
/// Sets the Service Start Type. True = Auto | False = Manual
2737
/// </summary>

0 commit comments

Comments
 (0)