-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProgramProperties.cs
65 lines (56 loc) · 2.53 KB
/
ProgramProperties.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using Serilog;
using System.Configuration;
using System.Diagnostics;
using Topshelf;
namespace ExchangeRatesUpdater
{
class ProgramProperties
{
static async Task Main(string[] args)
{
var configFilePath = ConfigurationManager.AppSettings["pathToConfigFile"];
FileSystemWatcher configWatcher = new FileSystemWatcher(Path.GetDirectoryName(configFilePath), Path.GetFileName(configFilePath));
configWatcher.NotifyFilter = NotifyFilters.LastWrite;
configWatcher.Changed += OnConfigFileChanged;
configWatcher.EnableRaisingEvents = true;
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.WriteTo.File(ConfigurationManager.AppSettings["pathToLogsFile"],
rollingInterval: RollingInterval.Day,
rollOnFileSizeLimit: true,
fileSizeLimitBytes: 200000,
retainedFileCountLimit: 5
)
.CreateLogger();
Log.Information($"\n" +
$"\n---------------------------------------------------------------------\n" +
$"\nLog from {DateTime.Now}\n" +
$"\n---------------------------------------------------------------------\n" +
$"\n");
var result = await Task.Run(() => HostFactory.Run(x =>
{
x.Service<App>(s =>
{
s.ConstructUsing(name => new App());
s.WhenStarted(tc => tc.Start().GetAwaiter().GetResult());
s.WhenStopped(tc => tc.Stop());
});
x.RunAsLocalSystem();
x.SetServiceName("ExchangeRatesUpdaterApp");
x.SetDisplayName("Exchange Rates Updater App");
x.SetDescription("App to update exchange rates automatically.");
}));
var exitCode = (int)Convert.ChangeType(result, result.GetTypeCode());
Log.Debug("Service execution result: {Result}", result);
Log.Debug("Exit code: {ExitCode}", exitCode);
Environment.ExitCode = exitCode;
}
private static void OnConfigFileChanged(object sender, FileSystemEventArgs e)
{
string assemblyPath = ConfigurationManager.AppSettings["pathToApplication"];
Process.Start(assemblyPath);
Environment.Exit(0);
}
}
}