Repository navigation
Expand file tree
/
Copy pathExtensions.Logs.cs
More file actions
582 lines (528 loc) · 22.7 KB
/
Copy pathExtensions.Logs.cs
File metadata and controls
582 lines (528 loc) · 22.7 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#region Related components
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Threading.Channels;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using net.vieapps.Components.Utility;
#endregion
namespace net.vieapps.Services
{
public static partial class Extensions
{
static string LogsPath { get; } = UtilityService.GetAppSetting("Path:Logs", "logs");
static int LogsQueueSize { get; } = Int32.TryParse(UtilityService.GetAppSetting("Logs:Size"), out var queueSize) && queueSize > 0 ? queueSize : 10240;
#region Service Logs
static Channel<LogInfo> ServiceLogs { get; } = Channel.CreateBounded<LogInfo>(new BoundedChannelOptions(Extensions.LogsQueueSize)
{
SingleReader = true,
SingleWriter = false,
FullMode = BoundedChannelFullMode.Wait
});
/// <summary>
/// Writes the service log into storages
/// </summary>
/// <param name="logInfo"></param>
/// <returns></returns>
public static bool WriteServiceLog(this LogInfo logInfo)
=> Extensions.ServiceLogs.Writer.TryWrite(logInfo);
/// <summary>
/// Writes the service log into storages
/// </summary>
/// <param name="logger"></param>
/// <param name="nodeID"></param>
/// <param name="correlationID"></param>
/// <param name="developerID"></param>
/// <param name="appID"></param>
/// <param name="logs"></param>
/// <param name="exception"></param>
/// <param name="serviceName"></param>
/// <param name="objectName"></param>
/// <param name="additional"></param>
/// <param name="mode"></param>
/// <returns></returns>
public static bool WriteServiceLog(ILogger logger, string nodeID, string correlationID, string developerID, string appID, List<string> logs, Exception exception, string serviceName, string objectName, string additional, LogLevel mode)
{
correlationID = correlationID ?? UtilityService.NewUUID;
logs = logs ?? new List<string>();
var stack = exception?.GetStack(false);
if (exception == null)
logs.ForEach(message => logger?.Log(mode, $"{message} [{correlationID}]"));
else
logs.ForEach(message => logger?.Log(LogLevel.Error, $"{message} [{correlationID}]"));
if (exception != null && exception is WampSharp.V2.Core.Contracts.WampException wampException)
{
var wampDetails = wampException.GetDetails();
if (wampDetails.Code > 0)
{
logger?.Log(LogLevel.Error, message: $"{wampDetails.Type}: {wampDetails.Message}\r\n{wampDetails.Stack} [{correlationID}]", exception: exception);
stack = $"{wampDetails.Type}: {wampDetails.Message}\r\n{wampDetails.Stack}";
}
else
logger?.Log(LogLevel.Error, $"{exception.Message} [{correlationID}]", exception);
logs.Add($"> Message: {wampDetails.Message}");
logs.Add($"> Type: {wampDetails.Type}");
}
else if (exception != null)
{
logger?.Log(LogLevel.Error, $"{exception.Message} [{correlationID}]", exception);
if (exception is AggregateException agg)
foreach (var inner in agg.Flatten().InnerExceptions)
{
logs.Add($"> Message: {inner.Message}");
logs.Add($"> Type: {inner.GetTypeName(true)}");
}
else
{
logs.Add($"> Message: {exception.Message}");
logs.Add($"> Type: {exception.GetTypeName(true)}");
}
}
if (!string.IsNullOrWhiteSpace(additional))
{
logs.Add(additional);
logger?.Log(exception != null ? LogLevel.Error : mode, $"{additional} [{correlationID}]");
}
return new LogInfo
{
ID = UtilityService.NewUUID,
Time = DateTime.Now,
CorrelationID = correlationID,
DeveloperID = developerID,
AppID = appID,
NodeID = nodeID ?? ServiceBase.ServiceComponent?.NodeID ?? Extensions.GetNodeID(),
ServiceName = (serviceName ?? "APIGateway").ToLower(),
ObjectName = (objectName ?? "").ToLower(),
Logs = logs.Join("\r\n"),
Stack = stack
}.WriteServiceLog();
}
/// <summary>
/// Writes the service log into storages
/// </summary>
/// <param name="correlationID"></param>
/// <param name="developerID"></param>
/// <param name="appID"></param>
/// <param name="logger"></param>
/// <param name="logs"></param>
/// <param name="exception"></param>
/// <param name="serviceName"></param>
/// <param name="objectName"></param>
/// <param name="mode"></param>
/// <returns></returns>
public static bool WriteServiceLog(string correlationID, string developerID, string appID, ILogger logger, List<string> logs, Exception exception = null, string serviceName = null, string objectName = null, LogLevel mode = LogLevel.Information)
=> Extensions.WriteServiceLog(logger, null, correlationID, developerID, appID, logs, exception, serviceName, objectName, null, mode);
/// <summary>
/// Writes the service log into storages
/// </summary>
/// <param name="requestInfo"></param>
/// <param name="logger"></param>
/// <param name="logs"></param>
/// <param name="exception"></param>
/// <param name="mode"></param>
/// <returns></returns>
public static bool WriteServiceLogs(this RequestInfo requestInfo, ILogger logger, List<string> logs, Exception exception = null, LogLevel mode = LogLevel.Information)
=> Extensions.WriteServiceLog(requestInfo.CorrelationID, requestInfo.Session?.DeveloperID, requestInfo.Session?.AppID, logger, logs, exception, requestInfo.ServiceName, requestInfo.ObjectName, mode);
/// <summary>
/// Writes the service log into storages
/// </summary>
/// <param name="requestInfo"></param>
/// <param name="logger"></param>
/// <param name="log"></param>
/// <param name="exception"></param>
/// <param name="mode"></param>
/// <returns></returns>
public static bool WriteServiceLogs(this RequestInfo requestInfo, ILogger logger, string log, Exception exception = null, LogLevel mode = LogLevel.Information)
=> requestInfo.WriteServiceLogs(logger, string.IsNullOrWhiteSpace(log) ? null : new List<string> { log }, exception, mode);
static Task ServiceLogsWritter { get; } = Task.Run(Extensions.WriteServiceLogsAsync);
static async Task WriteServiceLogsAsync()
{
while (await Extensions.ServiceLogs.Reader.WaitToReadAsync().ConfigureAwait(false))
{
while (Extensions.ServiceLogs.Reader.TryRead(out var logInfo))
try
{
await logInfo.SaveToFileAsync(Path.Combine(Extensions.LogsPath, $"zlogs.services.{logInfo.Time:yyyyMMddHHmmssffffff}.{logInfo.ID}.json")).ConfigureAwait(false);
}
catch { }
}
}
#endregion
#region Trace Logs
static Channel<TraceInfo> TraceLogs { get; } = Channel.CreateBounded<TraceInfo>(new BoundedChannelOptions(Extensions.LogsQueueSize)
{
SingleReader = true,
SingleWriter = false,
FullMode = BoundedChannelFullMode.Wait
});
/// <summary>
/// Starts new trace tracking
/// </summary>
/// <param name="parentID"></param>
/// <param name="correlationID"></param>
/// <param name="nodeID"></param>
/// <param name="serviceName"></param>
/// <param name="operation"></param>
/// <param name="kind"></param>
/// <param name="attributes"></param>
/// <returns></returns>
public static TraceInfo StartTrace(string parentID, string correlationID, string nodeID, string serviceName, string operation, string kind = "internal", Dictionary<string, object> attributes = null)
=> new TraceInfo
{
ID = UtilityService.NewUUID,
ParentID = parentID,
CorrelationID = correlationID,
NodeID = nodeID,
ServiceName = serviceName?.ToLower(),
Operation = operation,
StartTime = DateTime.Now,
Kind = kind,
Status = "unset",
Attributes = attributes ?? new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
};
/// <summary>
/// Completes trace tracking and write into storages
/// </summary>
/// <param name="traceInfo"></param>
/// <param name="status"></param>
/// <param name="statusDescription"></param>
/// <param name="attributes"></param>
/// <returns></returns>
public static TraceInfo Complete(this TraceInfo traceInfo, string status, string statusDescription = null, Dictionary<string, object> attributes = null)
{
traceInfo.EndTime = DateTime.Now;
traceInfo.DurationMs = (traceInfo.EndTime.Value - traceInfo.StartTime).TotalMilliseconds;
traceInfo.Status = status;
if (!string.IsNullOrWhiteSpace(statusDescription))
traceInfo.StatusDescription = statusDescription;
attributes?.ForEach(kvp => traceInfo.Attributes[kvp.Key] = kvp.Value);
Extensions.TraceLogs.Writer.TryWrite(traceInfo);
return traceInfo;
}
/// <summary>
/// Completes trace tracking and write into storages
/// </summary>
/// <param name="traceInfo"></param>
/// <param name="attributes"></param>
/// <returns></returns>
public static TraceInfo Complete(this TraceInfo traceInfo, Dictionary<string, object> attributes = null)
=> traceInfo.Complete("ok", null, attributes);
/// <summary>
/// Sets the identifier of parent trace tracking
/// </summary>
/// <param name="requestInfo"></param>
/// <param name="spanID"></param>
/// <returns></returns>
public static RequestInfo SetParentTraceID(this RequestInfo requestInfo, string spanID)
{
requestInfo.Extra["x-parent-trace-id"] = spanID;
return requestInfo;
}
/// <summary>
/// Gets the identifier of parent trace tracking
/// </summary>
/// <param name="requestInfo"></param>
/// <returns></returns>
public static string GetParentTraceID(this RequestInfo requestInfo)
=> requestInfo.Extra.TryGetValue("x-parent-trace-id", out var spanID) ? spanID : null;
/// <summary>
/// Gets the operation of trace tracking
/// </summary>
/// <param name="requestInfo"></param>
/// <returns></returns>
public static string GetTraceAction(this RequestInfo requestInfo)
{
var verb = (requestInfo.Verb ?? "Do").ToUpper();
switch (verb)
{
case "GET":
var objectIdentity = requestInfo.GetObjectIdentity();
return "search".IsEquals(objectIdentity) ? "SEARCH" : objectIdentity != null && (objectIdentity.IsValidUUID() || objectIdentity.IsEquals("refresh") || objectIdentity.IsEquals("cache")) ? "READ" : verb;
case "POST":
return "CREATE";
case "PUT":
return "UPDATE";
default:
return verb;
}
}
/// <summary>
/// Starts new trace tracking
/// </summary>
/// <param name="requestInfo"></param>
/// <param name="operation"></param>
/// <param name="kind"></param>
/// <param name="attributes"></param>
/// <returns></returns>
public static TraceInfo StartTrace(this RequestInfo requestInfo, string operation, string kind, Dictionary<string, object> attributes, bool autoSetParentTraceSpanID)
{
var traceInfo = Extensions.StartTrace(requestInfo.GetParentTraceID(), requestInfo.CorrelationID, ServiceBase.ServiceComponent?.NodeID ?? Extensions.GetNodeID(), requestInfo.ServiceName, operation, kind, attributes);
if (autoSetParentTraceSpanID)
requestInfo.SetParentTraceID(traceInfo.ID);
return traceInfo;
}
/// <summary>
/// Starts new trace tracking
/// </summary>
/// <param name="requestInfo"></param>
/// <param name="operation"></param>
/// <param name="kind"></param>
/// <returns></returns>
public static TraceInfo StartTrace(this RequestInfo requestInfo, string operation, string kind = "internal", bool autoSetParentTraceSpanID = false)
=> requestInfo.StartTrace(operation, kind, null, autoSetParentTraceSpanID);
/// <summary>
/// Starts new trace tracking
/// </summary>
/// <param name="requestInfo"></param>
/// <param name="traceInfo"></param>
/// <param name="operation"></param>
/// <param name="kind"></param>
/// <param name="autoSetParentTraceSpanID"></param>
/// <returns></returns>
public static TraceInfo StartTrace(this RequestInfo requestInfo, TraceInfo traceInfo, string operation, string kind = "internal", bool autoSetParentTraceSpanID = false)
{
traceInfo.Complete();
return requestInfo.StartTrace(operation, kind, autoSetParentTraceSpanID);
}
/// <summary>
/// Starts new trace tracking
/// </summary>
/// <param name="requestInfo"></param>
/// <param name="getOperation"></param>
/// <param name="autoSetParentTraceSpanID"></param>
/// <returns></returns>
public static TraceInfo StartTrace(this RequestInfo requestInfo, Func<RequestInfo, string> getOperation = null, bool autoSetParentTraceSpanID = true)
{
var operation = getOperation != null ? getOperation(requestInfo) : null;
return requestInfo.StartTrace(operation ?? $"{requestInfo.GetTraceAction()} {requestInfo.ObjectName?.ToArray(".").Last().GetCapitalizedFirstLetter()}", "downstream", null, autoSetParentTraceSpanID);
}
static Task TraceLogsWritter { get; } = Task.Run(Extensions.WriteTraceLogsAsync);
static async Task WriteTraceLogsAsync()
{
while (await Extensions.TraceLogs.Reader.WaitToReadAsync().ConfigureAwait(false))
{
while (Extensions.TraceLogs.Reader.TryRead(out var traceInfo))
try
{
await traceInfo.SaveToFileAsync(Path.Combine(Extensions.LogsPath, $"zlogs.traces.{DateTime.Now:yyyyMMddHHmmssffffff}.{traceInfo.ID}.json")).ConfigureAwait(false);
}
catch { }
}
}
#endregion
#region Usage Logs
static Channel<UsageInfo> UsageLogs { get; } = Channel.CreateBounded<UsageInfo>(new BoundedChannelOptions(Extensions.LogsQueueSize)
{
SingleReader = true,
SingleWriter = false,
FullMode = BoundedChannelFullMode.Wait
});
/// <summary>
/// Starts new resource usage tracking
/// </summary>
/// <param name="developerID"></param>
/// <param name="appID"></param>
/// <param name="userID"></param>
/// <param name="nodeID"></param>
/// <param name="correlationID"></param>
/// <param name="serviceName"></param>
/// <param name="objectName"></param>
/// <param name="operation"></param>
/// <param name="type"></param>
/// <param name="provider"></param>
/// <param name="model"></param>
/// <param name="systemID"></param>
/// <param name="traceID"></param>
/// <returns></returns>
public static UsageInfo StartUsage(string developerID, string appID, string userID, string nodeID, string correlationID, string serviceName, string objectName, string operation, string type, string provider = null, string model = null, string systemID = null, string traceID = null)
=> new UsageInfo
{
ID = UtilityService.NewUUID,
CorrelationID = correlationID,
TraceID = traceID,
DeveloperID = developerID,
AppID = appID,
UserID = userID,
NodeID = nodeID,
ServiceName = serviceName?.ToLower(),
ObjectName = objectName?.ToLower(),
SystemID = systemID,
Operation = operation,
StartTime = DateTime.Now,
Type = type,
Provider = provider,
Model = model,
Status = "unset",
Details = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
};
/// <summary>
/// Completes resource usage tracking and writes into storages
/// </summary>
/// <param name="usageInfo"></param>
/// <param name="status"></param>
/// <param name="details"></param>
/// <returns></returns>
public static UsageInfo Complete(this UsageInfo usageInfo, string status, Dictionary<string, object> details = null)
{
usageInfo.EndTime = DateTime.Now;
usageInfo.DurationMs = (usageInfo.EndTime.Value - usageInfo.StartTime).TotalMilliseconds;
usageInfo.Status = status;
details?.ForEach(kvp => usageInfo.Details[kvp.Key] = kvp.Value);
Extensions.UsageLogs.Writer.TryWrite(usageInfo);
return usageInfo;
}
/// <summary>
/// Completes resource usage tracking and writes into storages
/// </summary>
/// <param name="usageInfo"></param>
/// <param name="details"></param>
/// <returns></returns>
public static UsageInfo Complete(this UsageInfo usageInfo, Dictionary<string, object> details = null)
=> usageInfo.Complete("ok", details);
/// <summary>
/// Starts new resource usage tracking
/// </summary>
/// <param name="requestInfo"></param>
/// <param name="operation"></param>
/// <param name="type"></param>
/// <param name="provider"></param>
/// <param name="model"></param>
/// <param name="traceID"></param>
/// <returns></returns>
public static UsageInfo StartUsage(this RequestInfo requestInfo, string operation, string type, string provider = null, string model = null, string traceID = null)
=> Extensions.StartUsage(requestInfo.Session?.DeveloperID, requestInfo.Session?.AppID, requestInfo.Session?.User?.ID, ServiceBase.ServiceComponent?.NodeID ?? Extensions.GetNodeID(), requestInfo.CorrelationID, requestInfo.ServiceName, requestInfo.ObjectName, operation, type, provider, model, requestInfo.GetParameter("x-system-id"), traceID);
/// <summary>
/// Starts new resource usage tracking
/// </summary>
/// <param name="requestInfo"></param>
/// <param name="usageInfo"></param>
/// <param name="operation"></param>
/// <param name="type"></param>
/// <param name="provider"></param>
/// <param name="model"></param>
/// <param name="traceID"></param>
/// <returns></returns>
public static UsageInfo StartUsage(this RequestInfo requestInfo, UsageInfo usageInfo, string operation, string type, string provider = null, string model = null, string traceID = null)
{
usageInfo.Complete();
return requestInfo.StartUsage(operation, type, provider, model, traceID);
}
static Task UsageLogsWritter { get; } = Task.Run(Extensions.WriteUsageLogsAsync);
static async Task WriteUsageLogsAsync()
{
while (await Extensions.UsageLogs.Reader.WaitToReadAsync().ConfigureAwait(false))
{
while (Extensions.UsageLogs.Reader.TryRead(out var usageInfo))
try
{
await usageInfo.SaveToFileAsync(Path.Combine(Extensions.LogsPath, $"zlogs.usages.{DateTime.Now:yyyyMMddHHmmssffffff}.{usageInfo.ID}.json")).ConfigureAwait(false);
}
catch { }
}
}
#endregion
#region Audit Logs
static Channel<AuditInfo> AuditLogs { get; } = Channel.CreateBounded<AuditInfo>(new BoundedChannelOptions(Extensions.LogsQueueSize)
{
SingleReader = true,
SingleWriter = false,
FullMode = BoundedChannelFullMode.Wait
});
/// <summary>
/// Starts tracking a business-relevant auditable action.
/// </summary>
public static AuditInfo StartAudit(string correlationID, string traceID, string developerID, string appID, string actorType, string actorID, string nodeID, string serviceName, string objectName, string objectID, string systemID, string operation)
=> new AuditInfo
{
ID = UtilityService.NewUUID,
CorrelationID = correlationID,
TraceID = traceID,
DeveloperID = developerID,
AppID = appID,
ActorType = actorType,
ActorID = actorID,
NodeID = nodeID,
ServiceName = serviceName?.ToLower(),
ObjectName = objectName?.ToLower(),
ObjectID = objectID,
SystemID = systemID,
Operation = operation,
Status = "unset",
Changes = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase),
Details = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
};
/// <summary>
/// Starts an audit record from the request context.
/// </summary>
public static AuditInfo StartAudit(this RequestInfo requestInfo, string operation, string objectID = null, string systemID = null, string traceID = null, string actorType = "user", string actorID = null)
=> Extensions.StartAudit(requestInfo.CorrelationID, traceID, requestInfo.Session?.DeveloperID, requestInfo.Session?.AppID, actorType, actorID ?? ("user".IsEquals(actorType) ? requestInfo.Session?.User?.ID : null), ServiceBase.ServiceComponent?.NodeID ?? Extensions.GetNodeID(), requestInfo.ServiceName, requestInfo.ObjectName, objectID, systemID ?? requestInfo.GetParameter("x-system-id"), operation);
/// <summary>
/// Completes the audit information without automatically persisting it.
/// </summary>
public static AuditInfo Complete(this AuditInfo auditInfo, string status, Dictionary<string, object> changes = null, Dictionary<string, object> details = null)
{
if (auditInfo.Time != null)
throw new InvalidOperationException("The audit has already been completed.");
if (status != "ok" && status != "error" && status != "denied" && status != "canceled")
throw new ArgumentException("Invalid audit status.", nameof(status));
auditInfo.Time = DateTime.Now;
auditInfo.Status = status;
if (changes != null)
auditInfo.Changes = new Dictionary<string, object>(changes, StringComparer.OrdinalIgnoreCase);
if (details != null)
auditInfo.Details = new Dictionary<string, object>(details, StringComparer.OrdinalIgnoreCase);
return auditInfo;
}
/// <summary>
/// Completes and enqueues an auditable action.
/// </summary>
public static bool CompleteAndWrite(this AuditInfo auditInfo, string status = "ok", Dictionary<string, object> changes = null, Dictionary<string, object> details = null)
=> auditInfo.Complete(status, changes, details).WriteAuditLog();
/// <summary>
/// Completes and enqueues an auditable action.
/// </summary>
public static bool CompleteAndWrite(this AuditInfo auditInfo, Dictionary<string, object> changes, Dictionary<string, object> details)
=> auditInfo.CompleteAndWrite("ok", changes, details);
/// <summary>
/// Completes and enqueues an auditable action.
/// </summary>
public static bool CompleteAndWrite(this AuditInfo auditInfo, Dictionary<string, object> details)
=> auditInfo.CompleteAndWrite(null, details);
/// <summary>
/// Enqueues a completed audit record for file persistence.
/// </summary>
public static bool WriteAuditLog(this AuditInfo auditInfo)
=> auditInfo != null && auditInfo.Time != null && !string.IsNullOrWhiteSpace(auditInfo.ID) && !string.IsNullOrWhiteSpace(auditInfo.ServiceName) && !string.IsNullOrWhiteSpace(auditInfo.ObjectName) && !string.IsNullOrWhiteSpace(auditInfo.Operation) && !string.IsNullOrWhiteSpace(auditInfo.ActorType) && !string.IsNullOrWhiteSpace(auditInfo.ActorID) && !string.IsNullOrWhiteSpace(auditInfo.Status) && auditInfo.Status != "unset" && Extensions.AuditLogs.Writer.TryWrite(auditInfo);
static Task AuditLogsWritter { get; } = Task.Run(Extensions.WriteAuditLogsAsync);
static async Task WriteAuditLogsAsync()
{
while (await Extensions.AuditLogs.Reader.WaitToReadAsync().ConfigureAwait(false))
{
while (Extensions.AuditLogs.Reader.TryRead(out var auditInfo))
{
try
{
await auditInfo.SaveToFileAsync(Path.Combine(Extensions.LogsPath, $"zlogs.audits.{auditInfo.Time:yyyyMMddHHmmssffffff}.{auditInfo.ID}.json")).ConfigureAwait(false);
}
catch { }
}
}
}
#endregion
/// <summary>
/// Shutdowns the logs
/// </summary>
/// <returns></returns>
public static Task ShutdownLogsAsync()
{
Extensions.ServiceLogs.Writer.TryComplete();
Extensions.TraceLogs.Writer.TryComplete();
Extensions.UsageLogs.Writer.TryComplete();
Extensions.AuditLogs.Writer.TryComplete();
return Task.WhenAll(Extensions.ServiceLogsWritter, Extensions.TraceLogsWritter, Extensions.UsageLogsWritter, Extensions.AuditLogsWritter);
}
}
}