Projekt

Obecné

Profil

Stáhnout (3.65 KB) Statistiky
| Větev: | Tag: | Revize:
1
using LDClient.utils.loggers;
2
using Microsoft.Extensions.Configuration;
3

    
4
namespace LDClient.utils {
5
    internal class ConfigLoader {
6
        private const string LoggingSection = "Logging";
7
        private const string NetworkSection = "Network";
8
        private const string CacheSection = "Cache";
9
        private const string DDSection = "DebuggerDetection";
10

    
11
        #region Logger
12
        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
        public LogVerbosity LogVerbosityType { get; set; } = LogVerbosity.Full;
19

    
20
        public LogFlow LogFlowType { get; set; } = LogFlow.Console;
21
        #endregion
22

    
23
        #region Api
24
        public string ApiBaseAddress { get; set; }
25
        public string ApiUsbEndPoint { get; set; }
26
        public uint ApiPort { get; set; }
27

    
28
        #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
        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
        #endregion
44

    
45
        public ConfigLoader() {
46
            var configuration = new ConfigurationBuilder()
47
                .AddJsonFile("appsettings.json")
48
                .Build();
49
            ReadAllSettings(configuration);
50
        }
51

    
52
        private void ReadAllSettings(IConfigurationRoot configuration) {
53

    
54
            try {
55
                var logging = configuration.GetSection(LoggingSection);
56
                //TODO: Exception handling
57
                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

    
64
                var network = configuration.GetSection(NetworkSection);
65
                ApiBaseAddress = network["ApiBaseAddress"];
66
                ApiUsbEndPoint = network["ApiLDEndPoint"];
67
                ApiPort = uint.Parse(network["ApiPort"]);
68

    
69

    
70
                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
                T32Address = debugger["T32Address"];
79
                T32Port = int.Parse(debugger["T32Port"]);
80
                T32ProcessName = debugger["T32ProcessName"];
81
                T32InfoLocation = debugger["T32InfoLocation"];
82
                DetectionPeriod = uint.Parse(debugger["DetectionPeriod"]);
83

    
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
                Console.WriteLine("Error during reading of configuration occurred!" + e);
89
                throw new IOException("Reading of configuration file failed! " + e);
90
            }
91
        }
92
    }
93
}
(1-1/2)