Add ConfigMonitor instance to refresh/reload the config when it changes.

Added logger reference to increase logging around start/stop commands of the service.
#54
This commit is contained in:
Chris.Watts90@outlook.com 2017-03-01 16:51:44 +00:00
parent 40d4d7b1f7
commit e246c2e47c

View File

@ -2,6 +2,7 @@
using System.Diagnostics; using System.Diagnostics;
using System.ServiceProcess; using System.ServiceProcess;
using System.Threading; using System.Threading;
using Interfaces;
using Microsoft.Owin.Hosting; using Microsoft.Owin.Hosting;
namespace WindowsDataCenter namespace WindowsDataCenter
@ -16,7 +17,9 @@ namespace WindowsDataCenter
private IDisposable _webApp; private IDisposable _webApp;
private bool _stopMainWorkerThread; private bool _stopMainWorkerThread;
private Thread _mainWorkerThread; private Thread _mainWorkerThread;
private ILogger _logger;
private ConfigMonitor.ConfigMonitor _cfgWatcher;
public void Start() public void Start()
{ {
OnStart(new string[] {}); OnStart(new string[] {});
@ -29,9 +32,14 @@ namespace WindowsDataCenter
protected override void OnStart(string[] args) protected override void OnStart(string[] args)
{ {
var configPath = string.Concat(System.Reflection.Assembly.GetEntryAssembly().Location, ".config");
//Initialise the Ninject system. //Initialise the Ninject system.
var ninjectInstance = NinjectHelper.GetInstance(); var ninjectInstance = NinjectHelper.GetInstance();
_logger = NinjectHelper.GetInstance().Get<ILogger>();
_logger.Trace("Starting Data Center Service");
_cfgWatcher = new ConfigMonitor.ConfigMonitor(configPath);
_logger.Trace("Monitoring App.config for changes");
_mainWorkerThread = new Thread(MainWorkerThread) _mainWorkerThread = new Thread(MainWorkerThread)
{ {
IsBackground = false, IsBackground = false,
@ -44,14 +52,20 @@ namespace WindowsDataCenter
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.WriteLine(ex); _logger.Fatal(ex);
throw;
} }
} }
protected override void OnStop() protected override void OnStop()
{ {
_logger.Trace("Stopping Data Center Service");
_webApp.Dispose(); _webApp.Dispose();
_logger.Trace("Stopped WebApi");
_cfgWatcher.Stop();
_logger.Trace("Stopped Config Watcher");
_stopMainWorkerThread = true; _stopMainWorkerThread = true;
if (_mainWorkerThread != null && _mainWorkerThread.IsAlive) if (_mainWorkerThread != null && _mainWorkerThread.IsAlive)
{ {
@ -61,6 +75,7 @@ namespace WindowsDataCenter
_mainWorkerThread.Interrupt(); _mainWorkerThread.Interrupt();
} }
} }
_logger.Trace("Exiting..");
} }
private void MainWorkerThread() private void MainWorkerThread()