-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDebugLogger.cs
More file actions
73 lines (61 loc) · 1.77 KB
/
DebugLogger.cs
File metadata and controls
73 lines (61 loc) · 1.77 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
using System;
using System.Collections.Generic;
using FizzCode.DbTools.Common.Logger;
using Serilog;
using Serilog.Events;
namespace FizzCode.DbTools.TestBase;
public class DebugLogger(ILogger logger)
{
private readonly ILogger _logger = logger;
public void OnLog(object? sender, LogEventArgs args)
{
if (args.Exception != null)
OnException(sender, args);
var values = new List<object?>();
if(args.Arguments is not null)
values.AddRange(args.Arguments);
_logger.Write(
(LogEventLevel)args.Severity,
"[{Module}] " + args.Text,
values.ToArray()
);
}
private void OnException(object? sender, LogEventArgs args)
{
var opsErrors = new List<string>();
GetOpsMessages(args.Exception!, opsErrors);
foreach (var opsError in opsErrors)
{
OnLog(sender, new LogEventArgs()
{
Severity = LogSeverity.Fatal,
Text = opsError,
ForOps = true,
});
}
var lvl = 0;
var msg = "EXCEPTION: ";
var ex = args.Exception;
while (ex != null)
{
if (lvl > 0)
msg += "\nINNER EXCEPTION: ";
msg += ex.Message;
ex = ex.InnerException;
lvl++;
}
_logger.Fatal("[{Module}], {Message}", msg);
}
private void GetOpsMessages(Exception ex, List<string> messages)
{
if (ex.InnerException != null)
GetOpsMessages(ex.InnerException, messages);
if (ex is AggregateException aex)
{
foreach (var iex in aex.InnerExceptions)
{
GetOpsMessages(iex, messages);
}
}
}
}