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
        
11
        private const string LoggingSection = "Logging";
12
        private const string NetworkSection = "Network";
13
        private const string CacheSection = "Cache";
14
        private const string DdSection = "DebuggerDetection";
15

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

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

    
33
        #endregion
34

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

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

    
55
        #endregion
56

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

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

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

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

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

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