Projekt

Obecné

Profil

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

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

    
14
        #region Logger
15
        
16
        public int LogChunkSize { get; private set; }
17
        public int LogChunkMaxCount { get; private set; }
18
        public int LogArchiveMaxCount { get; private set; }
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
        public string F32RemExecutable { get; private set; }
47
        public uint FetchInfoMaxAttempts { get; private set;  }
48
        public uint FetchInfoAttemptPeriod { get; private set; }
49
        public string[] F32RemArguments { get; private set; }
50
        public int T32RemSuccessExitCode { get; private set; }
51
        public int T32RemWaitTimeoutMs { get; private set; }
52

    
53
        #endregion
54

    
55
        public ConfigLoader() {
56
            var configuration = new ConfigurationBuilder()
57
                .AddJsonFile("appsettings.json")
58
                .Build();
59
            ReadAllSettings(configuration);
60
        }
61

    
62
        private void ReadAllSettings(IConfiguration configuration) {
63

    
64
            try {
65
                var logging = configuration.GetSection(LoggingSection);
66
                //TODO: Exception handling
67
                LogChunkSize = int.Parse(logging["LogChunkSize"]);
68
                LogChunkMaxCount = int.Parse(logging["LogChunkMaxCount"]);
69
                LogArchiveMaxCount = int.Parse(logging["LogArchiveMaxCount"]);
70
                LogCleanupPeriod = int.Parse(logging["LogCleanupPeriod"]);
71
                LogFlowType = (LogFlow)int.Parse(logging["LogFlowType"]);
72
                LogVerbosityType = (LogVerbosity)int.Parse(logging["LogVerbosityType"]);
73

    
74
                var network = configuration.GetSection(NetworkSection);
75
                ApiBaseAddress = network["ApiBaseAddress"];
76
                ApiUsbEndPoint = network["ApiLDEndPoint"];
77
                ApiPort = uint.Parse(network["ApiPort"]);
78
                
79
                var cache = configuration.GetSection(CacheSection);
80
                RetryPeriod = uint.Parse(cache["RetryPeriod"]);
81
                MaxEntries = uint.Parse(cache["MaxEntries"]);
82
                MaxRetries = uint.Parse(cache["MaxRetries"]);
83
                CacheFileName = cache["CacheFileName"];
84
                
85
                var debugger = configuration.GetSection(DdSection);
86
                T32ProcessName = debugger["T32ProcessName"];
87
                T32InfoLocation = debugger["T32InfoLocation"];
88
                DetectionPeriod = uint.Parse(debugger["DetectionPeriod"]);
89
                F32RemExecutable = debugger["F32RemExecutable"];
90
                FetchInfoMaxAttempts = uint.Parse(debugger["FetchInfoMaxAttempts"]);
91
                FetchInfoAttemptPeriod = uint.Parse(debugger["FetchInfoAttemptPeriod"]);
92
                T32RemSuccessExitCode = int.Parse(debugger["T32RemSuccessExitCode"]);
93
                T32RemWaitTimeoutMs = int.Parse(debugger["T32RemWaitTimeoutMs"]);
94
                F32RemArguments = configuration.GetSection($"{DdSection}:F32RemCommands").GetChildren().Select(key => key.Value).ToArray();
95

    
96
                Console.WriteLine("Configuration successfully loaded!");
97
            } catch (FormatException e) {
98
                //Console.WriteLine("Error reading app settings");
99
                //TODO: remove stacktrace print in production
100
                Console.WriteLine("Error during reading of configuration occurred!" + e);
101
                throw new IOException("Reading of configuration file failed! " + e);
102
            }
103
        }
104
    }
105
}
    (1-1/1)