-
-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathSentryStructuredLogger.cs
More file actions
121 lines (103 loc) · 3.55 KB
/
SentryStructuredLogger.cs
File metadata and controls
121 lines (103 loc) · 3.55 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
using Microsoft.Extensions.Logging;
using Sentry.Extensibility;
using Sentry.Infrastructure;
namespace Sentry.Extensions.Logging;
internal sealed class SentryStructuredLogger : ILogger
{
private readonly string? _categoryName;
private readonly SentryLoggingOptions _options;
private readonly IHub _hub;
private readonly ISystemClock _clock;
private readonly SdkVersion _sdk;
internal SentryStructuredLogger(string categoryName, SentryLoggingOptions options, IHub hub, ISystemClock clock, SdkVersion sdk)
{
_categoryName = categoryName;
_options = options;
_clock = clock;
_hub = hub;
_sdk = sdk;
}
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
{
return NullDisposable.Instance;
}
public bool IsEnabled(LogLevel logLevel)
{
return _hub.IsEnabled
&& _options.EnableLogs
&& logLevel != LogLevel.None;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
if (!IsEnabled(logLevel))
{
return;
}
var timestamp = _clock.GetUtcNow();
SentryLog.GetTraceIdAndSpanId(_hub, out var traceId, out var spanId);
var level = logLevel.ToSentryLogLevel();
Debug.Assert(level != default);
string message;
try
{
message = formatter.Invoke(state, exception);
}
catch (FormatException e)
{
_options.DiagnosticLogger?.LogError(e, "Template string does not match the provided argument. The Log will be dropped.");
return;
}
string? template = null;
var parameters = ImmutableArray.CreateBuilder<KeyValuePair<string, object>>();
// see Microsoft.Extensions.Logging.FormattedLogValues
if (state is IReadOnlyList<KeyValuePair<string, object?>> formattedLogValues)
{
if (formattedLogValues.Count != 0)
{
parameters.Capacity = formattedLogValues.Count - 1;
}
foreach (var formattedLogValue in formattedLogValues)
{
if (formattedLogValue.Key == "{OriginalFormat}" && formattedLogValue.Value is string formattedString)
{
template = formattedString;
}
else if (formattedLogValue.Value is not null)
{
parameters.Add(new KeyValuePair<string, object>(formattedLogValue.Key, formattedLogValue.Value));
}
}
}
SentryLog log = new(timestamp, traceId, level, message)
{
Template = template,
Parameters = parameters.DrainToImmutable(),
ParentSpanId = spanId,
};
log.SetDefaultAttributes(_options, _hub.GetScope(), _sdk);
log.SetOrigin("auto.log.extensions_logging");
if (_categoryName is not null)
{
log.SetAttribute("category.name", _categoryName);
}
if (eventId.Name is not null || eventId.Id != 0)
{
log.SetAttribute("event.id", eventId.Id);
}
if (eventId.Name is not null)
{
log.SetAttribute("event.name", eventId.Name);
}
_hub.Logger.CaptureLog(log);
}
}
file sealed class NullDisposable : IDisposable
{
public static NullDisposable Instance { get; } = new NullDisposable();
private NullDisposable()
{
}
public void Dispose()
{
}
}