@@ -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 )
0 commit comments