Projekt

Obecné

Profil

Stáhnout (3.48 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 DebuggerAddress { get; set; }
39
        public int DebuggerPort { get; set; }
40
        public string DebuggerProcessName { get; set; }
41
        #endregion
42

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

    
50
        private void ReadAllSettings(IConfigurationRoot configuration) {
51

    
52
            try {
53
                var logging = configuration.GetSection(LoggingSection);
54
                //TODO: Exception handling
55
                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
                
62
                var network = configuration.GetSection(NetworkSection);
63
                ApiBaseAddress = network["ApiBaseAddress"];
64
                ApiUsbEndPoint = network["ApiLDEndPoint"];
65
                ApiPort = uint.Parse(network["ApiPort"]);
66

    
67

    
68
                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

    
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
                Console.WriteLine("Error during reading of configuration occurred!" + e);
85
                throw new IOException("Reading of configuration file failed! " + e);
86
            }
87
        }
88
    }
89
}
    (1-1/1)