Projekt

Obecné

Profil

Stáhnout (4.25 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 string F32RemArguments { get; private set; }
48
        public uint FetchInfoMaxAttempts { get; private set;  }
49
        public uint FetchInfoAttemptPeriod { get; private set; }
50
        
51
        #endregion
52

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

    
60
        private void ReadAllSettings(IConfiguration configuration) {
61

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

    
72
                var network = configuration.GetSection(NetworkSection);
73
                ApiBaseAddress = network["ApiBaseAddress"];
74
                ApiUsbEndPoint = network["ApiLDEndPoint"];
75
                ApiPort = uint.Parse(network["ApiPort"]);
76
                
77
                var cache = configuration.GetSection(CacheSection);
78
                RetryPeriod = uint.Parse(cache["RetryPeriod"]);
79
                MaxEntries = uint.Parse(cache["MaxEntries"]);
80
                MaxRetries = uint.Parse(cache["MaxRetries"]);
81
                CacheFileName = cache["CacheFileName"];
82
                
83
                var debugger = configuration.GetSection(DdSection);
84
                T32ProcessName = debugger["T32ProcessName"];
85
                T32InfoLocation = debugger["T32InfoLocation"];
86
                DetectionPeriod = uint.Parse(debugger["DetectionPeriod"]);
87
                F32RemExecutable = debugger["F32RemExecutable"];
88
                F32RemArguments = debugger["F32RemArguments"];
89
                FetchInfoMaxAttempts = uint.Parse(debugger["FetchInfoMaxAttempts"]);
90
                FetchInfoAttemptPeriod = uint.Parse(debugger["FetchInfoAttemptPeriod"]);
91

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