Projekt

Obecné

Profil

Stáhnout (3.65 KB) Statistiky
| Větev: | Tag: | Revize:
1 74bd1e40 Pultak
using LDClient.utils.loggers;
2 d0cf9476 Pultak
using Microsoft.Extensions.Configuration;
3
4 74bd1e40 Pultak
namespace LDClient.utils {
5
    internal class ConfigLoader {
6
        private const string LoggingSection = "Logging";
7 58cf6a6f Pultak
        private const string NetworkSection = "Network";
8 c2cc7813 Pultak
        private const string CacheSection = "Cache";
9
        private const string DDSection = "DebuggerDetection";
10 d0cf9476 Pultak
11 c2cc7813 Pultak
        #region Logger
12 d0cf9476 Pultak
        public int LogChunkSize { get; set; }
13
        public int LogChunkMaxCount { get; set; }
14
        public int LogArchiveMaxCount { get; set; }
15
16
        public int LogCleanupPeriod { get; set; }
17
18 74bd1e40 Pultak
        public LogVerbosity LogVerbosityType { get; set; } = LogVerbosity.Full;
19 d0cf9476 Pultak
20 74bd1e40 Pultak
        public LogFlow LogFlowType { get; set; } = LogFlow.Console;
21 c2cc7813 Pultak
        #endregion
22 d0cf9476 Pultak
23 c2cc7813 Pultak
        #region Api
24 58cf6a6f Pultak
        public string ApiBaseAddress { get; set; }
25
        public string ApiUsbEndPoint { get; set; }
26
        public uint ApiPort { get; set; }
27
28 c2cc7813 Pultak
        #endregion
29
30
        #region Cache
31
        public string CacheFileName { get; set; }
32
        public uint MaxRetries { get; set; }
33
        public uint MaxEntries { get; set; }
34
        public uint RetryPeriod { get; set; }
35
        #endregion
36
37
        #region Detection
38 914776bd Pultak
        public string T32Address { get; set; }
39
        public int T32Port { get; set; }
40
        public string T32ProcessName { get; set; }
41
        public uint DetectionPeriod { get; set; }
42
        public string T32InfoLocation { get; set; }
43 c2cc7813 Pultak
        #endregion
44 58cf6a6f Pultak
45 74bd1e40 Pultak
        public ConfigLoader() {
46 d0cf9476 Pultak
            var configuration = new ConfigurationBuilder()
47
                .AddJsonFile("appsettings.json")
48
                .Build();
49
            ReadAllSettings(configuration);
50
        }
51
52 74bd1e40 Pultak
        private void ReadAllSettings(IConfigurationRoot configuration) {
53 d0cf9476 Pultak
54
            try {
55 74bd1e40 Pultak
                var logging = configuration.GetSection(LoggingSection);
56 d0cf9476 Pultak
                //TODO: Exception handling
57 74bd1e40 Pultak
                LogChunkSize = int.Parse(logging["LogChunkSize"]);
58
                LogChunkMaxCount = int.Parse(logging["LogChunkMaxCount"]);
59
                LogArchiveMaxCount = int.Parse(logging["LogArchiveMaxCount"]);
60
                LogCleanupPeriod = int.Parse(logging["LogCleanupPeriod"]);
61
                LogFlowType = (LogFlow)int.Parse(logging["LogFlowType"]);
62
                LogVerbosityType = (LogVerbosity)int.Parse(logging["LogVerbosityType"]);
63 914776bd Pultak
64 58cf6a6f Pultak
                var network = configuration.GetSection(NetworkSection);
65
                ApiBaseAddress = network["ApiBaseAddress"];
66
                ApiUsbEndPoint = network["ApiLDEndPoint"];
67
                ApiPort = uint.Parse(network["ApiPort"]);
68
69 d0cf9476 Pultak
70 c2cc7813 Pultak
                var cache = configuration.GetSection(CacheSection);
71
                RetryPeriod = uint.Parse(cache["RetryPeriod"]);
72
                MaxEntries = uint.Parse(cache["MaxEntries"]);
73
                MaxRetries = uint.Parse(cache["MaxRetries"]);
74
                CacheFileName = cache["CacheFileName"];
75
76
77
                var debugger = configuration.GetSection(DDSection);
78 914776bd Pultak
                T32Address = debugger["T32Address"];
79
                T32Port = int.Parse(debugger["T32Port"]);
80
                T32ProcessName = debugger["T32ProcessName"];
81
                T32InfoLocation = debugger["T32InfoLocation"];
82
                DetectionPeriod = uint.Parse(debugger["DetectionPeriod"]);
83 d0cf9476 Pultak
84
                Console.WriteLine("Configuration successfully loaded!");
85
            } catch (FormatException e) {
86
                //Console.WriteLine("Error reading app settings");
87
                //TODO: remove stacktrace print in production
88 74bd1e40 Pultak
                Console.WriteLine("Error during reading of configuration occurred!" + e);
89 d0cf9476 Pultak
                throw new IOException("Reading of configuration file failed! " + e);
90
            }
91
        }
92
    }
93
}