-
Notifications
You must be signed in to change notification settings - Fork 0
/
HyperLogger.cs
367 lines (329 loc) · 13.7 KB
/
HyperLogger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
using Gurock.SmartInspect;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text.Json;
using System.Threading;
namespace SmartLogger
{
public enum Level
{
Trace,
Debug,
Verbose,
Info,
Message,
Warn,
Warning,
Error,
Fatal,
Control,
Off,
Standby
}
public sealed class HyperLogger
{
private static HyperLogger instance = null;
private IDictionary<string, HyperLog> sessionLookup;
private List<HyperLog> sessionList;
private SmartInspect _SmartInspect;
private ColorManager _ColorManager;
private static readonly object padlock = new object();
private Configuration _Configuration;
private string _ConfigFile;
private FileSystemWatcher _ConfigFileWatcher;
private static bool InStartup;
HyperLogger()
{
}
public static Configuration HyperConfiguration()
{
if (instance is HyperLogger)
{
return instance._Configuration;
}
else
{
return new Configuration(true); // production config as default
}
}
public static List<HyperLog> FindMaskedAreas(string mask)
{
string[] masks = mask.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
bool negate;
bool wildcard;
char lastChar;
List<HyperLog> result = new List<HyperLog>();
foreach (string submask in masks)
{
string workingmask = submask.Trim();
workingmask = workingmask.ToLower();
if (workingmask == "")
{
continue;
}
negate = workingmask[0] == '!';
if (negate)
{
workingmask = workingmask.Substring(1);
}
if (workingmask == "")
{
continue;
}
if (workingmask == "*")
{
foreach (HyperLog hyperlog in instance.sessionList)
{
if (negate)
{
result.Remove(hyperlog);
}
else
{
result.Add(hyperlog);
}
}
continue;
}
lastChar = workingmask[workingmask.Length - 1];
wildcard = (lastChar == '*') || (lastChar == '.');
if (wildcard)
{
workingmask = workingmask.Substring(0, workingmask.Length - 1);
}
bool found;
foreach (HyperLog hyperlog in instance.sessionList)
{
found = false;
if (wildcard)
{
found = hyperlog.Name.ToLower().StartsWith(workingmask);
}
else
{
found = hyperlog.Name.ToLower() == workingmask;
}
if (found)
{
if (negate)
{
result.Remove(hyperlog);
}
else
{
result.Add(hyperlog);
}
}
}
continue;
}
return result;
}
private static void ApplyRuleAreaItem(RuleAreaItem ruleAreaItem)
{
List<HyperLog> logs;
logs = FindMaskedAreas(ruleAreaItem.Mask);
foreach (HyperLog hyperlog in logs)
{
Bootstrap.Log.LogDebug($">>>>>>>>Filtered Area [{hyperlog.Name}]");
hyperlog.SetHyperLogLevels(ruleAreaItem.LoggingLevel, ruleAreaItem.DetailLevel);
}
}
public static void ReapplyRules()
{
Bootstrap.Log.LogColored(Color.Green, $"Reappying Rules count: {instance._Configuration.Rules.Count}");
foreach (Rule rule in instance._Configuration.Rules)
{
Bootstrap.Log.LogColored(Color.YellowGreen, $"Rule [{rule.Name}] Enabled: {rule.Enabled.ToString()}");
if (rule.Enabled)
{
foreach (RuleAreaItem item in rule.RuleAreaItems)
{
if (item.Enabled)
{
Bootstrap.Log.LogColored(Color.LightYellow, $">>>Rule item[{item.Mask}]");
ApplyRuleAreaItem(item);
}
}
}
}
}
public static string CurrentStateToJSONstring()
{
List<RuntimeHyperArea> areas = new List<RuntimeHyperArea>();
RuntimeHyperArea area;
foreach (HyperLog hyperlog in instance.sessionList)
{
area = new RuntimeHyperArea();
area.Name = hyperlog.Name;
area.HighLevel = hyperlog.HighLevel();
area.LowLevel = hyperlog.LowLevel();
if (hyperlog.Log != null)
{
area.Enabled = hyperlog.Log.SiSession.Active.ToString();
area.Color = hyperlog.Log.SiSession.Color.Name;
area.Level = hyperlog.Log.SiSession.Level.ToString();
}
else
{
area.Enabled = "False";
area.Color = "Black";
area.Level = "Off";
}
areas.Add(area);
}
var options = new JsonSerializerOptions
{
WriteIndented = true // For pretty-printing the JSON
};
return JsonSerializer.Serialize(areas, options);
}
public static void UpdateStateFromJSONstring(string json)
{
// Ensure to specify the type to deserialize to, in this case, List<RuntimeHyperArea>
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true // This option helps with deserializing if the JSON property names don't match case with your C# model properties
};
List<RuntimeHyperArea> areas = JsonSerializer.Deserialize<List<RuntimeHyperArea>>(json, options);
if (areas != null) // Always a good practice to check for null after deserialization
{
foreach (RuntimeHyperArea area in areas)
{
// Your logic here
}
}
}
private static void OnChanged(object sender, FileSystemEventArgs e)
{
if (InStartup)
{
return;
}
Bootstrap.Log.LogColored(Color.Black, $"OnChanged Event");
if (e.ChangeType != WatcherChangeTypes.Changed)
{
return;
}
Bootstrap.Log.LogColored(Color.Brown, $"On Config file Changed Event");
int retries = 10;
Configuration updatedConfig = null;
Thread.Sleep(100);
while (retries > 0)
{
try
{
updatedConfig = Configuration.LoadConfiguration(instance._ConfigFile);
break;
}
catch (Exception ex)
{
retries--;
Thread.Sleep(100);
updatedConfig = null;
Bootstrap.Log.LogException($"Issue reading file Retry{retries}", ex);
}
}
if (!(updatedConfig is Configuration))
{
Bootstrap.Log.LogWarning($"Issues encountered when reloading the configuration file {instance._ConfigFile}. The current configuration is still being used.");
return;
}
instance._Configuration = updatedConfig;
HyperLog hyperlog;
foreach (Area area in instance._Configuration.Areas)
{
if (instance.sessionLookup.TryGetValue(area.Name.ToLower(), out hyperlog))
{
hyperlog.UpdateFromArea(area);
}
}
ReapplyRules();
}
public static string ConfigFile(string exeFile = "")
{
return LogFilePicker.ConfigFile(exeFile);
}
public static HyperLog MakeHyperLog(string name)
{
string key;
HyperLog x;
lock (padlock)
{
if (instance == null)
{
InStartup = true;
Bootstrap.Log.LogMessage("Starting Up Logger");
instance = new HyperLogger();
instance._ConfigFile = ConfigFile();
instance._Configuration = Configuration.LoadConfiguration(instance._ConfigFile);
if (instance._Configuration.Rules.Count == 0) // In case rules manually removed from config file
{
instance._Configuration.AddDefaultRules(true); // production default
}
Bootstrap.Log.LogColored(Color.MintCream, System.IO.Path.GetDirectoryName(instance._ConfigFile));
instance._ConfigFileWatcher = new FileSystemWatcher(System.IO.Path.GetDirectoryName(instance._ConfigFile) + "\\");
Bootstrap.Log.LogColored(Color.MintCream, System.IO.Path.GetFileName(instance._ConfigFile));
instance._ConfigFileWatcher.Filter = System.IO.Path.GetFileName(instance._ConfigFile);
instance._ConfigFileWatcher.Changed += OnChanged;
instance._ConfigFileWatcher.EnableRaisingEvents = true;
instance._SmartInspect = new SmartInspect(Configuration.PlainAppFilename());
instance.sessionLookup = new Dictionary<string, HyperLog>();
instance.sessionList = new List<HyperLog>();
instance._SmartInspect.AppName = instance._Configuration.AppName;
instance._SmartInspect.Enabled = instance._Configuration.Enabled;
instance._SmartInspect.Resolution = instance._Configuration.Resolution.ToLower() == "high" ? ClockResolution.High : ClockResolution.Standard;
instance._SmartInspect.DefaultLevel = HyperAreaBase.XlatLevel(instance._Configuration.DefaultLevel);
instance._SmartInspect.SessionDefaults.Level = HyperAreaBase.XlatLevel(instance._Configuration.SessionDefaults);
instance._SmartInspect.Connections = instance._Configuration.ConfigurationString();
//Bootstrap.Log.LogString(Gurock.SmartInspect.Level.Warning, "Connections", instance._SmartInspect.Connections);
instance._SmartInspect.Enabled = true;
instance._ColorManager = new ColorManager();
Bootstrap.Log.LogString(Gurock.SmartInspect.Level.Debug, "Connections", instance._SmartInspect.Connections);
Bootstrap.Log.LogInt(Gurock.SmartInspect.Level.Debug, "Area Count", instance._Configuration.Areas.Count);
Bootstrap.Log.LogInt(Gurock.SmartInspect.Level.Debug, "Rule Count", instance._Configuration.Rules.Count);
Bootstrap.Log.LogInt(Gurock.SmartInspect.Level.Debug, "Connection Count", instance._Configuration.Connections.Count);
foreach (Area area in instance._Configuration.Areas)
{
key = area.Name.ToLower();
Bootstrap.Log.LogString("Area From Config", area.Name);
x = new HyperLog(instance._SmartInspect, area);
instance.sessionLookup.Add(key, x);
instance.sessionList.Add(x);
instance._ColorManager.NextColor(); // bump to reduce duplicate colors.
}
ReapplyRules();
// Bootstrap.Log.LogString(Gurock.SmartInspect.Level.Warning, "XNew Connections", instance._Configuration.ConfigurationString());
// Log the messages
InStartup = false;
}
if (!string.IsNullOrEmpty(name))
{
key = name.ToLower();
if (!instance.sessionLookup.TryGetValue(key, out x))
{
Area newArea = new Area();
newArea.Name = name;
newArea.AreaColor = instance._ColorManager.NextColor().Name;
x = new HyperLog(instance._SmartInspect, newArea);
instance.sessionLookup.Add(key, x);
instance.sessionList.Add(x);
x.High.Warning(name, Color.Fuchsia);
instance._Configuration.Areas.Add(newArea);
ReapplyRules();
}
//File.WriteAllText(@"f:\json.txt", CurrentStateToJSONstring());
return x;
}
return null;
}
}
public static HyperLog MakeHyperLog(object unit)
{
string name = unit.GetType().ToString();
return MakeHyperLog(name);
}
}
}