1
|
using LDClient.utils.loggers;
|
2
|
using Microsoft.Extensions.Configuration;
|
3
|
|
4
|
namespace LDClient.utils {
|
5
|
|
6
|
/// <summary>
|
7
|
/// This class loads up the configuration file (appsettingss.json).
|
8
|
/// </summary>
|
9
|
internal class ConfigLoader {
|
10
|
|
11
|
/// <summary>
|
12
|
/// Status code indicating a successful termination of an application.
|
13
|
/// </summary>
|
14
|
private const int ErrorExitCode = 1;
|
15
|
|
16
|
/// <summary>
|
17
|
/// Path to the configuration file.
|
18
|
/// </summary>
|
19
|
private const string ConfigFile = "appsettings.json";
|
20
|
|
21
|
/// <summary>
|
22
|
/// Name of the logging section defined in the config file.
|
23
|
/// </summary>
|
24
|
private const string LoggingSection = "Logging";
|
25
|
|
26
|
/// <summary>
|
27
|
/// Name of the network section defined in the config file.
|
28
|
/// </summary>
|
29
|
private const string NetworkSection = "Network";
|
30
|
|
31
|
/// <summary>
|
32
|
/// Name of the cache section defined in the config file.
|
33
|
/// </summary>
|
34
|
private const string CacheSection = "Cache";
|
35
|
|
36
|
/// <summary>
|
37
|
/// Name of the detection section defined in the config file.
|
38
|
/// </summary>
|
39
|
private const string DdSection = "DebuggerDetection";
|
40
|
|
41
|
|
42
|
public string[] T32ApiCommands { get; private set; } = null!;
|
43
|
public string T32ApiAddress { get; private set; } = null!;
|
44
|
public string T32ApiPort { get; private set; } = null!;
|
45
|
public string T32ApiPacketLen { get; private set; } = null!;
|
46
|
|
47
|
#region Logger
|
48
|
|
49
|
/// <summary>
|
50
|
/// Maximum size of the log file (it will start to rotate when this limit is reached).
|
51
|
/// </summary>
|
52
|
public int LogChunkSize { get; private set; }
|
53
|
|
54
|
/// <summary>
|
55
|
/// Number of files to be created until there will be zipped up.
|
56
|
/// </summary>
|
57
|
public int LogChunkMaxCount { get; private set; }
|
58
|
|
59
|
/// <summary>
|
60
|
/// Maximum number of zip files
|
61
|
/// </summary>
|
62
|
public int LogArchiveMaxCount { get; private set; }
|
63
|
|
64
|
/// <summary>
|
65
|
/// Time after which the last logs will be cleaned up.
|
66
|
/// </summary>
|
67
|
public int LogCleanupPeriod { get; private set; }
|
68
|
|
69
|
/// <summary>
|
70
|
/// Level of verbosity.
|
71
|
/// </summary>
|
72
|
public LogVerbosity LogVerbosityType { get; private set; } = LogVerbosity.Full;
|
73
|
|
74
|
/// <summary>
|
75
|
/// Logger flow type.
|
76
|
/// </summary>
|
77
|
public LogFlow LogFlowType { get; private set; } = LogFlow.Console;
|
78
|
|
79
|
#endregion
|
80
|
|
81
|
#region Api
|
82
|
|
83
|
/// <summary>
|
84
|
/// URL to the API (it can be an IP address or a domain name if a DNS server is being used).
|
85
|
/// </summary>
|
86
|
public string ApiBaseAddress { get; private set; } = null!;
|
87
|
|
88
|
/// <summary>
|
89
|
/// Path to the API (e.g. /api/v1/ld-logs).
|
90
|
/// </summary>
|
91
|
public string ApiUsbEndPoint { get; private set; } = null!;
|
92
|
|
93
|
/// <summary>
|
94
|
/// Number of the port that the API runs on.
|
95
|
/// </summary>
|
96
|
public uint ApiPort { get; private set; }
|
97
|
|
98
|
#endregion
|
99
|
|
100
|
#region Cache
|
101
|
|
102
|
/// <summary>
|
103
|
/// Name of the cache (a directory of this name will be created).
|
104
|
/// </summary>
|
105
|
public string CacheFileName { get; private set; } = null!;
|
106
|
|
107
|
/// <summary>
|
108
|
/// Maximum number of payloads (entries) to be sent to the server at a time.
|
109
|
/// </summary>
|
110
|
public uint MaxRetries { get; private set; }
|
111
|
|
112
|
/// <summary>
|
113
|
/// Maximum number of entries that can be stored in the database.
|
114
|
/// </summary>
|
115
|
public uint MaxEntries { get; private set; }
|
116
|
|
117
|
/// <summary>
|
118
|
/// Period (how often) a certain number of entries will be resent to the server.
|
119
|
/// </summary>
|
120
|
public uint RetryPeriod { get; private set; }
|
121
|
|
122
|
#endregion
|
123
|
|
124
|
#region Detection
|
125
|
|
126
|
/// <summary>
|
127
|
/// Name of the process to be detected (the application programmers use to connect to the debugger).
|
128
|
/// </summary>
|
129
|
public string T32ProcessName { get; private set; } = null!;
|
130
|
|
131
|
/// <summary>
|
132
|
/// How often the application checks if there is the process (T32ProcessName) running on the PC.
|
133
|
/// </summary>
|
134
|
public uint DetectionPeriod { get; private set; }
|
135
|
|
136
|
/// <summary>
|
137
|
/// Location of the generated .txt file containing all information about a debugger.
|
138
|
/// </summary>
|
139
|
public string T32InfoLocation { get; private set; } = null!;
|
140
|
|
141
|
/// <summary>
|
142
|
/// Path to the t32rem.exe which is used to send commands to a debugger.
|
143
|
/// </summary>
|
144
|
public string T32RemExecutable { get; private set; } = null!;
|
145
|
|
146
|
/// <summary>
|
147
|
/// How many times the application attempts to check if there
|
148
|
/// has been a .txt file generated containing all the desired information.
|
149
|
/// </summary>
|
150
|
public uint FetchInfoMaxAttempts { get; private set; }
|
151
|
|
152
|
/// <summary>
|
153
|
/// Period in milliseconds after which the application tries to locate and parse the .txt file.
|
154
|
/// </summary>
|
155
|
public uint FetchInfoAttemptPeriod { get; private set; }
|
156
|
|
157
|
/// <summary>
|
158
|
/// Arguments (commands) sent to the t32rem.exe file.
|
159
|
/// </summary>
|
160
|
public string[] T32RemArguments { get; private set; } = null!;
|
161
|
|
162
|
/// <summary>
|
163
|
/// Status code indication successful execution of the t32rem.exe file.
|
164
|
/// </summary>
|
165
|
public int T32RemSuccessExitCode { get; private set; }
|
166
|
|
167
|
/// <summary>
|
168
|
/// Timeout of the execution of t32rem.exe (when sending one command).
|
169
|
/// </summary>
|
170
|
public int T32RemWaitTimeoutMs { get; private set; }
|
171
|
|
172
|
#endregion
|
173
|
|
174
|
/// <summary>
|
175
|
/// Creates an instance of the class.
|
176
|
/// </summary>
|
177
|
public ConfigLoader() {
|
178
|
// Create a new config builder to read the configuration file.
|
179
|
var configuration = new ConfigurationBuilder()
|
180
|
.AddJsonFile(ConfigFile)
|
181
|
.Build();
|
182
|
|
183
|
// Parse the logger section.
|
184
|
ReadLoggerSection(configuration);
|
185
|
|
186
|
// Parse the api section.
|
187
|
ReadApiSection(configuration);
|
188
|
|
189
|
// Parse the cache section.
|
190
|
ReadCacheSection(configuration);
|
191
|
|
192
|
// Parse the detection section.
|
193
|
ReadDebuggerSection(configuration);
|
194
|
|
195
|
Console.WriteLine("Configuration successfully loaded!");
|
196
|
}
|
197
|
|
198
|
/// <summary>
|
199
|
/// Parses the logging section of the configuration file.
|
200
|
/// </summary>
|
201
|
/// <param name="configuration">configuration</param>
|
202
|
private void ReadLoggerSection(IConfiguration configuration) {
|
203
|
try {
|
204
|
var logging = configuration.GetSection(LoggingSection);
|
205
|
LogChunkSize = int.Parse(logging["LogChunkSize"]);
|
206
|
LogChunkMaxCount = int.Parse(logging["LogChunkMaxCount"]);
|
207
|
LogArchiveMaxCount = int.Parse(logging["LogArchiveMaxCount"]);
|
208
|
LogCleanupPeriod = int.Parse(logging["LogCleanupPeriod"]);
|
209
|
LogFlowType = (LogFlow)int.Parse(logging["LogFlowType"]);
|
210
|
LogVerbosityType = (LogVerbosity)int.Parse(logging["LogVerbosityType"]);
|
211
|
} catch (Exception e) {
|
212
|
Console.WriteLine(e.Message);
|
213
|
Environment.Exit(ErrorExitCode);
|
214
|
}
|
215
|
}
|
216
|
|
217
|
/// <summary>
|
218
|
/// Parses the api section of the configuration file.
|
219
|
/// </summary>
|
220
|
/// <param name="configuration">configuration</param>
|
221
|
private void ReadApiSection(IConfiguration configuration) {
|
222
|
try {
|
223
|
var network = configuration.GetSection(NetworkSection);
|
224
|
ApiBaseAddress = network["ApiBaseAddress"];
|
225
|
ApiUsbEndPoint = network["ApiLDEndPoint"];
|
226
|
ApiPort = uint.Parse(network["ApiPort"]);
|
227
|
} catch (Exception e) {
|
228
|
Console.WriteLine(e.Message);
|
229
|
Environment.Exit(ErrorExitCode);
|
230
|
}
|
231
|
}
|
232
|
|
233
|
/// <summary>
|
234
|
/// Parses the cache section of the configuration file.
|
235
|
/// </summary>
|
236
|
/// <param name="configuration">configuration</param>
|
237
|
private void ReadCacheSection(IConfiguration configuration) {
|
238
|
try {
|
239
|
var cache = configuration.GetSection(CacheSection);
|
240
|
RetryPeriod = uint.Parse(cache["RetryPeriod"]);
|
241
|
MaxEntries = uint.Parse(cache["MaxEntries"]);
|
242
|
MaxRetries = uint.Parse(cache["MaxRetries"]);
|
243
|
CacheFileName = cache["CacheFileName"];
|
244
|
} catch (Exception e) {
|
245
|
Console.WriteLine(e.Message);
|
246
|
Environment.Exit(ErrorExitCode);
|
247
|
}
|
248
|
}
|
249
|
|
250
|
/// <summary>
|
251
|
/// Parses the detection section of the configuration file.
|
252
|
/// </summary>
|
253
|
/// <param name="configuration">configuration</param>
|
254
|
private void ReadDebuggerSection(IConfiguration configuration) {
|
255
|
try {
|
256
|
var debugger = configuration.GetSection(DdSection);
|
257
|
T32ProcessName = debugger["T32ProcessName"];
|
258
|
T32InfoLocation = debugger["T32InfoLocation"];
|
259
|
DetectionPeriod = uint.Parse(debugger["DetectionPeriod"]);
|
260
|
T32RemExecutable = debugger["T32RemExecutable"];
|
261
|
FetchInfoMaxAttempts = uint.Parse(debugger["FetchInfoMaxAttempts"]);
|
262
|
FetchInfoAttemptPeriod = uint.Parse(debugger["FetchInfoAttemptPeriod"]);
|
263
|
T32RemSuccessExitCode = int.Parse(debugger["T32RemSuccessExitCode"]);
|
264
|
T32RemWaitTimeoutMs = int.Parse(debugger["T32RemWaitTimeoutMs"]);
|
265
|
T32RemArguments = configuration.GetSection($"{DdSection}:T32RemArguments").GetChildren().Select(key => key.Value).ToArray();
|
266
|
T32ApiCommands = configuration.GetSection($"{DdSection}:T32ApiCommands").GetChildren().Select(key => key.Value).ToArray();
|
267
|
T32ApiAddress = debugger["T32ApiAddress"];
|
268
|
T32ApiPort = debugger["T32ApiPort"];
|
269
|
T32ApiPacketLen = debugger["T32ApiPacketLen"];
|
270
|
|
271
|
} catch (Exception e) {
|
272
|
Console.WriteLine(e);
|
273
|
Environment.Exit(ErrorExitCode);
|
274
|
}
|
275
|
}
|
276
|
}
|
277
|
}
|