Projekt

Obecné

Profil

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

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

    
13
        #region Logger
14
        
15
        public int LogChunkSize { get; private set; }
16
        public int LogChunkMaxCount { get; private set; }
17
        public int LogArchiveMaxCount { get; private set; }
18

    
19
        public int LogCleanupPeriod { get; private set; }
20
        public LogVerbosity LogVerbosityType { get; private set; } = LogVerbosity.Full;
21
        public LogFlow LogFlowType { get; private set; } = LogFlow.Console;
22
        
23
        #endregion
24

    
25
        #region Api
26
        
27
        public string ApiBaseAddress { get; private set; }
28
        public string ApiUsbEndPoint { get; private set; }
29
        public uint ApiPort { get; private set; }
30

    
31
        #endregion
32

    
33
        #region Cache
34
        
35
        public string CacheFileName { get; private set; }
36
        public uint MaxRetries { get; private set; }
37
        public uint MaxEntries { get; private set; }
38
        public uint RetryPeriod { get; private set; }
39
        
40
        #endregion
41

    
42
        #region Detection
43
        public string T32ProcessName { get; private set; }
44
        public uint DetectionPeriod { get; private set; }
45
        public string T32InfoLocation { get; private set; }
46
        #endregion
47

    
48
        public ConfigLoader() {
49
            var configuration = new ConfigurationBuilder()
50
                .AddJsonFile("appsettings.json")
51
                .Build();
52
            ReadAllSettings(configuration);
53
        }
54

    
55
        private void ReadAllSettings(IConfiguration configuration) {
56

    
57
            try {
58
                var logging = configuration.GetSection(LoggingSection);
59
                //TODO: Exception handling
60
                LogChunkSize = int.Parse(logging["LogChunkSize"]);
61
                LogChunkMaxCount = int.Parse(logging["LogChunkMaxCount"]);
62
                LogArchiveMaxCount = int.Parse(logging["LogArchiveMaxCount"]);
63
                LogCleanupPeriod = int.Parse(logging["LogCleanupPeriod"]);
64
                LogFlowType = (LogFlow)int.Parse(logging["LogFlowType"]);
65
                LogVerbosityType = (LogVerbosity)int.Parse(logging["LogVerbosityType"]);
66

    
67
                var network = configuration.GetSection(NetworkSection);
68
                ApiBaseAddress = network["ApiBaseAddress"];
69
                ApiUsbEndPoint = network["ApiLDEndPoint"];
70
                ApiPort = uint.Parse(network["ApiPort"]);
71

    
72

    
73
                var cache = configuration.GetSection(CacheSection);
74
                RetryPeriod = uint.Parse(cache["RetryPeriod"]);
75
                MaxEntries = uint.Parse(cache["MaxEntries"]);
76
                MaxRetries = uint.Parse(cache["MaxRetries"]);
77
                CacheFileName = cache["CacheFileName"];
78

    
79

    
80
                var debugger = configuration.GetSection(DdSection);
81
                T32ProcessName = debugger["T32ProcessName"];
82
                T32InfoLocation = debugger["T32InfoLocation"];
83
                DetectionPeriod = uint.Parse(debugger["DetectionPeriod"]);
84

    
85
                Console.WriteLine("Configuration successfully loaded!");
86
            } catch (FormatException e) {
87
                //Console.WriteLine("Error reading app settings");
88
                //TODO: remove stacktrace print in production
89
                Console.WriteLine("Error during reading of configuration occurred!" + e);
90
                throw new IOException("Reading of configuration file failed! " + e);
91
            }
92
        }
93
    }
94
}
(1-1/2)