Projekt

Obecné

Profil

Stáhnout (3.48 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 58cf6a6f Pultak
        public string DebuggerAddress { get; set; }
39
        public int DebuggerPort { get; set; }
40 c2cc7813 Pultak
        public string DebuggerProcessName { get; set; }
41
        #endregion
42 58cf6a6f Pultak
43 74bd1e40 Pultak
        public ConfigLoader() {
44 d0cf9476 Pultak
            var configuration = new ConfigurationBuilder()
45
                .AddJsonFile("appsettings.json")
46
                .Build();
47
            ReadAllSettings(configuration);
48
        }
49
50 74bd1e40 Pultak
        private void ReadAllSettings(IConfigurationRoot configuration) {
51 d0cf9476 Pultak
52
            try {
53 74bd1e40 Pultak
                var logging = configuration.GetSection(LoggingSection);
54 d0cf9476 Pultak
                //TODO: Exception handling
55 74bd1e40 Pultak
                LogChunkSize = int.Parse(logging["LogChunkSize"]);
56
                LogChunkMaxCount = int.Parse(logging["LogChunkMaxCount"]);
57
                LogArchiveMaxCount = int.Parse(logging["LogArchiveMaxCount"]);
58
                LogCleanupPeriod = int.Parse(logging["LogCleanupPeriod"]);
59
                LogFlowType = (LogFlow)int.Parse(logging["LogFlowType"]);
60
                LogVerbosityType = (LogVerbosity)int.Parse(logging["LogVerbosityType"]);
61 58cf6a6f Pultak
                
62
                var network = configuration.GetSection(NetworkSection);
63
                ApiBaseAddress = network["ApiBaseAddress"];
64
                ApiUsbEndPoint = network["ApiLDEndPoint"];
65
                ApiPort = uint.Parse(network["ApiPort"]);
66
67 d0cf9476 Pultak
68 c2cc7813 Pultak
                var cache = configuration.GetSection(CacheSection);
69
                RetryPeriod = uint.Parse(cache["RetryPeriod"]);
70
                MaxEntries = uint.Parse(cache["MaxEntries"]);
71
                MaxRetries = uint.Parse(cache["MaxRetries"]);
72
                CacheFileName = cache["CacheFileName"];
73
74
75
                var debugger = configuration.GetSection(DDSection);
76
                DebuggerAddress = debugger["DebuggerAddress"];
77
                DebuggerPort = int.Parse(debugger["DebuggerPort"]);
78
                DebuggerProcessName = debugger["DebuggerProcessName"];
79 d0cf9476 Pultak
80
                Console.WriteLine("Configuration successfully loaded!");
81
            } catch (FormatException e) {
82
                //Console.WriteLine("Error reading app settings");
83
                //TODO: remove stacktrace print in production
84 74bd1e40 Pultak
                Console.WriteLine("Error during reading of configuration occurred!" + e);
85 d0cf9476 Pultak
                throw new IOException("Reading of configuration file failed! " + e);
86
            }
87
        }
88
    }
89
}