Projekt

Obecné

Profil

Stáhnout (5.29 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 int ErrorExitCode = 1;
9
        private const string ConfigFile = "appsettings.json";
10
        private const string LoggingSection = "Logging";
11
        private const string NetworkSection = "Network";
12
        private const string CacheSection = "Cache";
13
        private const string DdSection = "DebuggerDetection";
14

    
15
        #region Logger
16
        
17
        public int LogChunkSize { get; private set; }
18
        public int LogChunkMaxCount { get; private set; }
19
        public int LogArchiveMaxCount { get; private set; }
20
        public int LogCleanupPeriod { get; private set; }
21
        public LogVerbosity LogVerbosityType { get; private set; } = LogVerbosity.Full;
22
        public LogFlow LogFlowType { get; private set; } = LogFlow.Console;
23
        
24
        #endregion
25

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

    
32
        #endregion
33

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

    
43
        #region Detection
44
        public string T32ProcessName { get; private set; } = null!;
45
        public uint DetectionPeriod { get; private set; }
46
        public string T32InfoLocation { get; private set; } = null!;
47
        public string F32RemExecutable { get; private set; } = null!;
48
        public uint FetchInfoMaxAttempts { get; private set;  }
49
        public uint FetchInfoAttemptPeriod { get; private set; }
50
        public string[] F32RemArguments { get; private set; } = null!;
51
        public int T32RemSuccessExitCode { get; private set; }
52
        public int T32RemWaitTimeoutMs { get; private set; }
53

    
54
        #endregion
55

    
56
        public ConfigLoader() {
57
            var configuration = new ConfigurationBuilder()
58
                .AddJsonFile(ConfigFile)
59
                .Build();
60

    
61
            ReadLoggerSection(configuration);
62
            ReadApiSection(configuration);
63
            ReadCacheSection(configuration);
64
            ReadDebuggerSection(configuration);
65
            
66
            Console.WriteLine("Configuration successfully loaded!");
67
        }
68

    
69
        private void ReadLoggerSection(IConfiguration configuration) {
70
            try {
71
                var logging = configuration.GetSection(LoggingSection);
72
                LogChunkSize = int.Parse(logging["LogChunkSize"]);
73
                LogChunkMaxCount = int.Parse(logging["LogChunkMaxCount"]);
74
                LogArchiveMaxCount = int.Parse(logging["LogArchiveMaxCount"]);
75
                LogCleanupPeriod = int.Parse(logging["LogCleanupPeriod"]);
76
                LogFlowType = (LogFlow)int.Parse(logging["LogFlowType"]);
77
                LogVerbosityType = (LogVerbosity)int.Parse(logging["LogVerbosityType"]);
78
            } catch (Exception e) {
79
                Console.WriteLine(e.Message);
80
                Environment.Exit(ErrorExitCode);
81
            }
82
        }
83

    
84
        private void ReadApiSection(IConfiguration configuration) {
85
            try {
86
                var network = configuration.GetSection(NetworkSection);
87
                ApiBaseAddress = network["ApiBaseAddress"];
88
                ApiUsbEndPoint = network["ApiLDEndPoint"];
89
                ApiPort = uint.Parse(network["ApiPort"]);
90
            } catch (Exception e) {
91
                Console.WriteLine(e.Message);
92
                Environment.Exit(ErrorExitCode);
93
            }
94
        }
95

    
96
        private void ReadCacheSection(IConfiguration configuration) {
97
            try {
98
                var cache = configuration.GetSection(CacheSection);
99
                RetryPeriod = uint.Parse(cache["RetryPeriod"]);
100
                MaxEntries = uint.Parse(cache["MaxEntries"]);
101
                MaxRetries = uint.Parse(cache["MaxRetries"]);
102
                CacheFileName = cache["CacheFileName"];
103
            } catch (Exception e) {
104
                Console.WriteLine(e.Message);
105
                Environment.Exit(ErrorExitCode);
106
            }
107
        }
108

    
109
        private void ReadDebuggerSection(IConfiguration configuration) {
110
            try {
111
                var debugger = configuration.GetSection(DdSection);
112
                T32ProcessName = debugger["T32ProcessName"];
113
                T32InfoLocation = debugger["T32InfoLocation"];
114
                DetectionPeriod = uint.Parse(debugger["DetectionPeriod"]);
115
                F32RemExecutable = debugger["F32RemExecutable"];
116
                FetchInfoMaxAttempts = uint.Parse(debugger["FetchInfoMaxAttempts"]);
117
                FetchInfoAttemptPeriod = uint.Parse(debugger["FetchInfoAttemptPeriod"]);
118
                T32RemSuccessExitCode = int.Parse(debugger["T32RemSuccessExitCode"]);
119
                T32RemWaitTimeoutMs = int.Parse(debugger["T32RemWaitTimeoutMs"]);
120
                F32RemArguments = configuration.GetSection($"{DdSection}:F32RemArguments").GetChildren().Select(key => key.Value).ToArray();
121
            } catch (Exception e) {
122
                Console.WriteLine(e);
123
                Environment.Exit(ErrorExitCode);
124
            }
125
        }
126
    }
127
}
    (1-1/1)