-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructLog.cs
More file actions
87 lines (70 loc) · 3.16 KB
/
StructLog.cs
File metadata and controls
87 lines (70 loc) · 3.16 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
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using StructLog.Interfaces;
using StructLog.Models;
using System.Text.Json;
namespace StructLog
{
public class StructLog<T> : IStructLog<T> where T : class
{
private readonly ILogger<T> _logger;
private readonly StructLogOptions _options;
private readonly IEnumerable<ILogEnricher> _enrichers;
private static readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true };
public StructLog(
ILogger<T> logger,
IOptions<StructLogOptions> options,
IEnumerable<ILogEnricher> enrichers)
{
ArgumentNullException.ThrowIfNull(logger);
ArgumentNullException.ThrowIfNull(options);
ArgumentNullException.ThrowIfNull(enrichers);
_logger = logger;
_options = options.Value;
_enrichers = enrichers;
}
public IDisposable? BeginScope(IDictionary<string, object> context)
=> _logger.BeginScope(context);
public void Critical(string message, IEventCode eventCode, Exception? ex = null, object? data = null)
=> Log(LogLevel.Critical, message, eventCode, ex, data);
public void Error(string message, IEventCode eventCode, Exception? ex = null, object? data = null)
=> Log(LogLevel.Error, message, eventCode, ex, data);
public void Warning(string message, IEventCode eventCode, object? data = null)
=> Log(LogLevel.Warning, message, eventCode, null, data);
public void Info(string message, IEventCode eventCode, object? data = null)
=> Log(LogLevel.Information, message, eventCode, null, data);
public void Debug(string message, IEventCode eventCode, object? data = null)
=> Log(LogLevel.Debug, message, eventCode, null, data);
public void Trace(string message, IEventCode eventCode, object? data = null)
=> Log(LogLevel.Trace, message, eventCode, null, data);
private sealed record LogEntry(
string EventCode,
string EventDescription,
string Message,
IDictionary<string, object> Context,
object? Data);
private void Log(LogLevel level, string message, IEventCode eventCode, Exception? exception, object? data)
{
if (!_logger.IsEnabled(level))
return;
var contextData = new Dictionary<string, object>();
if (_options.EnableEnrichers)
foreach (var enricher in _enrichers)
enricher.Enrich(contextData);
var logEntry = new LogEntry(
EventCode: eventCode.Code,
EventDescription: eventCode.Description,
Message: message,
Context: contextData,
Data: data);
var json = JsonSerializer.Serialize(logEntry, _jsonOptions);
using (_logger.BeginScope(contextData))
_logger.Log(
level,
default,
logEntry,
exception,
(_, __) => json);
}
}
}