-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathSentryLogger.cs
217 lines (187 loc) · 6.55 KB
/
SentryLogger.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
using Microsoft.Extensions.Logging;
using Sentry.Infrastructure;
namespace Sentry.Extensions.Logging;
internal sealed class SentryLogger : ILogger
{
private readonly IHub _hub;
private readonly ISystemClock _clock;
private readonly SentryLoggingOptions _options;
internal string CategoryName { get; }
internal SentryLogger(
string categoryName,
SentryLoggingOptions options,
ISystemClock clock,
IHub hub)
{
CategoryName = categoryName;
_options = options;
_clock = clock;
_hub = hub;
}
#if NET8_0_OR_GREATER
public IDisposable BeginScope<TState>(TState state) where TState : notnull
=> _hub.PushScope(state);
#else
public IDisposable BeginScope<TState>(TState state) => _hub.PushScope(state);
#endif
public bool IsEnabled(LogLevel logLevel)
=> _hub.IsEnabled
&& logLevel != LogLevel.None
&& (logLevel >= _options.MinimumBreadcrumbLevel
|| logLevel >= _options.MinimumEventLevel);
public void Log<TState>(
LogLevel logLevel,
EventId eventId,
TState state,
Exception? exception,
Func<TState, Exception?, string>? formatter)
{
if (!IsEnabled(logLevel))
{
return;
}
var message = formatter?.Invoke(state, exception);
if (ShouldCaptureEvent(logLevel, eventId, exception))
{
var @event = CreateEvent(logLevel, eventId, state, exception, message, CategoryName);
_ = _hub.CaptureEvent(@event);
// Capturing exception events adds a breadcrumb automatically... we don't want to add another one
if (exception != null)
{
return;
}
}
if (ShouldAddBreadcrumb(logLevel, eventId, exception))
{
var data = eventId.ToDictionaryOrNull();
if (exception != null && message != null)
{
// Exception.Message won't be used as Breadcrumb message
// Avoid losing it by adding as data:
data ??= new Dictionary<string, string>();
data.Add("exception_message", exception.Message);
}
_hub.AddBreadcrumb(
_clock,
(message ?? exception?.Message)!,
CategoryName,
null,
data,
logLevel.ToBreadcrumbLevel());
}
}
internal static SentryEvent CreateEvent<TState>(
LogLevel logLevel,
EventId id,
TState state,
Exception? exception,
string? message,
string category)
{
exception?.SetSentryMechanism("SentryLogger", handled: !IsUnhandledWasmException(id));
var @event = new SentryEvent(exception)
{
Logger = category,
Message = message,
Level = logLevel.ToSentryLevel(),
};
if (state is IEnumerable<KeyValuePair<string, object>> pairs)
{
foreach (var property in pairs)
{
if (property.Key == "{OriginalFormat}" && property.Value is string template)
{
// Original format found, use Sentry logEntry interface
@event.Message = new SentryMessage
{
Formatted = message,
Message = template
};
continue;
}
switch (property.Value)
{
case string stringTagValue:
@event.SetTag(property.Key, stringTagValue);
break;
case Guid guidTagValue when guidTagValue != Guid.Empty:
@event.SetTag(property.Key, guidTagValue.ToString());
break;
case Enum enumValue:
@event.SetTag(property.Key, enumValue.ToString());
break;
default:
{
if (property.Value?.GetType().IsPrimitive == true)
{
@event.SetTag(property.Key, Convert.ToString(property.Value, CultureInfo.InvariantCulture)!);
}
break;
}
}
}
}
var tuple = id.ToTupleOrNull();
if (tuple.HasValue)
{
@event.SetTag(tuple.Value.name, tuple.Value.value);
}
return @event;
}
private bool ShouldCaptureEvent(
LogLevel logLevel,
EventId eventId,
Exception? exception)
=> _options.MinimumEventLevel != LogLevel.None
&& logLevel >= _options.MinimumEventLevel
&& !IsFromSentry()
&& !IsEfExceptionMessage(eventId)
&& _options.Filters.All(
f => !f.Filter(
CategoryName,
logLevel,
eventId,
exception));
private bool ShouldAddBreadcrumb(
LogLevel logLevel,
EventId eventId,
Exception? exception)
=> _options.MinimumBreadcrumbLevel != LogLevel.None
&& logLevel >= _options.MinimumBreadcrumbLevel
&& !IsFromSentry()
&& !IsEfExceptionMessage(eventId)
&& _options.Filters.All(
f => !f.Filter(
CategoryName,
logLevel,
eventId,
exception));
private bool IsFromSentry()
{
if (string.Equals(CategoryName, "Sentry", StringComparison.Ordinal))
{
return true;
}
#if DEBUG
if (CategoryName.StartsWith("Sentry.Samples.", StringComparison.Ordinal))
{
return false;
}
#endif
return CategoryName.StartsWith("Sentry.", StringComparison.Ordinal);
}
internal static bool IsEfExceptionMessage(EventId eventId)
{
return eventId.Name is
"Microsoft.EntityFrameworkCore.Update.SaveChangesFailed" or
"Microsoft.EntityFrameworkCore.Query.QueryIterationFailed" or
"Microsoft.EntityFrameworkCore.Query.InvalidIncludePathError" or
"Microsoft.EntityFrameworkCore.Update.OptimisticConcurrencyException";
}
internal static bool IsUnhandledWasmException(EventId eventId)
{
return eventId.Name is
"ExceptionRenderingComponent" or
"NavigationFailed";
}
}