-
-
Notifications
You must be signed in to change notification settings - Fork 207
/
SentryTarget.cs
509 lines (443 loc) · 16.2 KB
/
SentryTarget.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
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
namespace Sentry.NLog;
/// <summary>
/// Sentry NLog Target.
/// </summary>
[Target("Sentry")]
public sealed class SentryTarget : TargetWithContext
{
// For testing:
internal Func<IHub> HubAccessor { get; }
private readonly ISystemClock _clock;
private IDisposable? _sdkDisposable;
internal static readonly SdkVersion NameAndVersion = typeof(SentryTarget).Assembly.GetNameAndVersion();
internal static readonly string AdditionalGroupingKeyProperty = "AdditionalGroupingKey";
private static readonly string ProtocolPackageName = "nuget:" + NameAndVersion.Name;
/// <summary>
/// Creates a new instance of <see cref="SentryTarget"/>.
/// </summary>
public SentryTarget() : this(new SentryNLogOptions())
{
}
/// <summary>
/// Creates a new instance of <see cref="SentryTarget"/>.
/// </summary>
public SentryTarget(SentryNLogOptions options)
: this(
options,
() => HubAdapter.Instance,
null,
SystemClock.Clock)
{
}
internal SentryTarget(SentryNLogOptions options, Func<IHub> hubAccessor, IDisposable? sdkInstance, ISystemClock clock)
{
Options = options;
HubAccessor = hubAccessor;
_clock = clock;
// Overrides default layout. Still will be explicitly overwritten if manually configured in the
// NLog.config file.
Layout = "${message}";
BreadcrumbCategory = Options.BreadcrumbCategoryLayout ?? "${logger}";
IncludeEventProperties = true;
if (sdkInstance != null)
{
_sdkDisposable = sdkInstance;
}
}
/// <summary>
/// Options for both the <see cref="SentryTarget"/> and the sentry sdk itself.
/// </summary>
public SentryNLogOptions Options { get; }
/// <summary>
/// Add any desired additional tags that will be sent with every message.
/// </summary>
[ArrayParameter(typeof(TargetPropertyWithContext), "tag")]
public IList<TargetPropertyWithContext> Tags => Options.Tags;
/// <summary>
/// Configured layout for Data Source Name of a given project in Sentry
/// </summary>
public Layout? Dsn
{
get => Options.DsnLayout;
set => Options.DsnLayout = value;
}
/// <summary>
/// Configured layout for application Release version to Sentry
/// </summary>
public Layout? Release
{
get => Options.ReleaseLayout;
set => Options.ReleaseLayout = value;
}
/// <summary>
/// Configured layout for application Environment to Sentry
/// </summary>
public Layout? Environment
{
get => Options.EnvironmentLayout;
set => Options.EnvironmentLayout = value;
}
/// <summary>
/// An optional layout specific to breadcrumbs. If not set, uses the same layout as the standard <see cref="TargetWithContext.Layout"/>.
/// </summary>
public Layout? BreadcrumbLayout
{
get => Options.BreadcrumbLayout ?? Layout;
set => Options.BreadcrumbLayout = value;
}
/// <summary>
/// Configured layout for application Environment to Sentry
/// </summary>
public Layout? BreadcrumbCategory
{
get => Options.BreadcrumbCategoryLayout;
set => Options.BreadcrumbCategoryLayout = value;
}
/// <summary>
/// Minimum log level for events to trigger a send to Sentry. Defaults to <see cref="M:LogLevel.Error" />.
/// </summary>
public string MinimumEventLevel
{
get => Options.MinimumEventLevel?.ToString() ?? LogLevel.Off.ToString();
set => Options.MinimumEventLevel = LogLevel.FromString(value);
}
/// <summary>
/// Minimum log level to be included in the breadcrumb. Defaults to <see cref="M:LogLevel.Info" />.
/// </summary>
public string MinimumBreadcrumbLevel
{
get => Options.MinimumBreadcrumbLevel?.ToString() ?? LogLevel.Off.ToString();
set => Options.MinimumBreadcrumbLevel = LogLevel.FromString(value);
}
/// <summary>
/// Whether the NLog integration should initialize the SDK.
/// </summary>
/// <remarks>
/// By default, if a DSN is provided to the NLog integration it will initialize the SDK.
/// This might be not ideal when using multiple integrations in case you want another one doing the Init.
/// </remarks>
public bool InitializeSdk
{
get => Options.InitializeSdk;
set => Options.InitializeSdk = value;
}
/// <summary>
/// Set this to <see langword="true" /> to ignore log messages that don't contain an exception.
/// </summary>
public bool IgnoreEventsWithNoException
{
get => Options.IgnoreEventsWithNoException;
set => Options.IgnoreEventsWithNoException = value;
}
/// <summary>
/// Determines whether event properties will be sent to sentry as Tags or not.
/// Defaults to <see langword="false" />.
/// </summary>
public bool IncludeEventPropertiesAsTags
{
get => Options.IncludeEventPropertiesAsTags;
set => Options.IncludeEventPropertiesAsTags = value;
}
/// <summary>
/// Determines whether or not to include event-level data as data in breadcrumbs for future errors.
/// Defaults to <see langword="false" />.
/// </summary>
public bool IncludeEventDataOnBreadcrumbs
{
get => Options.IncludeEventDataOnBreadcrumbs;
set => Options.IncludeEventDataOnBreadcrumbs = value;
}
/// <summary>
/// How many seconds to wait after triggering <see cref="LogManager.Shutdown()"/> before just shutting down the
/// Sentry sdk.
/// </summary>
public int ShutdownTimeoutSeconds
{
get => Options.ShutdownTimeoutSeconds;
set => Options.ShutdownTimeoutSeconds = value;
}
/// <summary>
/// How long to wait for the flush to finish, in seconds. Defaults to 2 seconds.
/// </summary>
public int FlushTimeoutSeconds
{
get => (int)Options.FlushTimeout.TotalSeconds;
set => Options.FlushTimeout = TimeSpan.FromSeconds(value);
}
/// <summary>
/// Optionally configure one or more parts of the user information to be rendered dynamically from an NLog layout
/// </summary>
public SentryNLogUser? User
{
get => Options.User;
set => Options.User = value;
}
/// <inheritdoc />
protected override void CloseTarget()
{
_sdkDisposable?.Dispose();
base.CloseTarget();
}
/// <inheritdoc />
protected override void InitializeTarget()
{
// If a layout has been configured on the options, replace the default logger.
if (Options.Layout != null)
{
Layout = Options.Layout;
}
base.InitializeTarget();
if (InternalLogger.IsDebugEnabled || InternalLogger.IsInfoEnabled || InternalLogger.IsWarnEnabled || InternalLogger.IsErrorEnabled || InternalLogger.IsFatalEnabled)
{
var existingLogger = Options.DiagnosticLogger;
if (existingLogger is not NLogDiagnosticLogger)
{
Options.DiagnosticLogger = new NLogDiagnosticLogger(existingLogger);
}
Options.Debug = true;
}
var customDsn = Dsn?.Render(LogEventInfo.CreateNullEvent());
if (!string.IsNullOrEmpty(customDsn))
{
Options.Dsn = customDsn;
}
var customRelease = Release?.Render(LogEventInfo.CreateNullEvent());
if (!string.IsNullOrEmpty(customRelease))
{
Options.Release = customRelease;
}
var customEnvironment = Environment?.Render(LogEventInfo.CreateNullEvent());
if (!string.IsNullOrEmpty(customEnvironment))
{
Options.Environment = customEnvironment;
}
// If the sdk is not there, set it on up.
if (InitializeSdk && _sdkDisposable == null)
{
_sdkDisposable = SentrySdk.Init(Options);
}
if (!HubAccessor().IsEnabled)
{
InternalLogger.Info("Sentry(Name={0}): Hub not enabled", Name);
}
}
/// <inheritdoc />
protected override void FlushAsync(AsyncContinuation asyncContinuation)
{
_ = HubAccessor()
.FlushAsync(Options.FlushTimeout)
.ContinueWith(t => asyncContinuation(t.Exception));
}
private static AsyncLocal<bool> isReentrant = new();
/// <summary>
/// <para>
/// If the event level >= the <see cref="MinimumEventLevel"/>, the
/// <paramref name="logEvent"/> is captured as an event by sentry.
/// </para>
/// <para>
/// If the event level is >= the <see cref="MinimumBreadcrumbLevel"/>, the event is added
/// as a breadcrumb to the Sentry Sdk.
/// </para>
/// <para>
/// If sentry is not enabled, this is a No-op.
/// </para>
/// </summary>
/// <param name="logEvent">The event that is being logged.</param>
/// <inheritdoc />
protected override void Write(LogEventInfo logEvent)
{
//TODO: check if can really be null
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
if (logEvent == null)
{
return;
}
if (isReentrant.Value)
{
Options.DiagnosticLogger?.LogError($"Reentrant log event detected. Logging when inside the scope of another log event can cause a StackOverflowException. LogEventInfo.Message:{logEvent.Message}");
return;
}
isReentrant.Value = true;
try
{
InnerWrite(logEvent);
}
catch (Exception exception)
{
Options.DiagnosticLogger?.LogError(exception, "Failed to write log event");
throw;
}
finally
{
isReentrant.Value = false;
}
}
private void InnerWrite(LogEventInfo logEvent)
{
if (logEvent.Message == null)
{
return;
}
var hub = HubAccessor();
if (!hub.IsEnabled)
{
return;
}
var exception = logEvent.Exception;
var shouldOnlyLogExceptions = exception == null && IgnoreEventsWithNoException;
var shouldIncludeProperties = ContextProperties?.Count > 0 || ShouldIncludeProperties(logEvent);
if (logEvent.Level >= Options.MinimumEventLevel && !shouldOnlyLogExceptions)
{
var formatted = RenderLogEvent(Layout, logEvent);
var template = logEvent.Message;
var evt = new SentryEvent(exception)
{
Message = new SentryMessage
{
Formatted = formatted,
Message = template
},
Logger = logEvent.LoggerName,
Level = logEvent.Level.ToSentryLevel(),
Release = Options.Release,
Environment = Options.Environment,
User = GetUser(logEvent) ?? new SentryUser(),
};
if (evt.Sdk is { } sdk)
{
sdk.Name = Constants.SdkName;
sdk.Version = NameAndVersion.Version;
if (NameAndVersion.Version is { } version)
{
sdk.AddPackage(ProtocolPackageName, version);
}
}
if (Tags.Count > 0 || IncludeEventPropertiesAsTags && logEvent.HasProperties)
{
evt.SetTags(GetTagsFromLogEvent(logEvent));
}
if (shouldIncludeProperties)
{
var contextProps = GetAllProperties(logEvent);
evt.SetExtras(contextProps);
if (contextProps.TryGetValue(AdditionalGroupingKeyProperty, out var additionalGroupingKey)
&& additionalGroupingKey is string groupingKey)
{
var overridenFingerprint = evt.Fingerprint.ToList();
if (!evt.Fingerprint.Any())
{
overridenFingerprint.Add("{{ default }}");
}
overridenFingerprint.Add(groupingKey);
evt.SetFingerprint(overridenFingerprint);
}
}
_ = hub.CaptureEvent(evt);
}
// Whether or not it was sent as event, add breadcrumb so the next event includes it
if (logEvent.Level >= Options.MinimumBreadcrumbLevel)
{
var breadcrumbFormatted = RenderLogEvent(BreadcrumbLayout, logEvent);
var breadcrumbCategory = RenderLogEvent(BreadcrumbCategory, logEvent);
if (string.IsNullOrEmpty(breadcrumbCategory))
{
breadcrumbCategory = null;
}
var message = string.IsNullOrWhiteSpace(breadcrumbFormatted)
? exception?.Message ?? logEvent.FormattedMessage
: breadcrumbFormatted;
IDictionary<string, string>? data = null;
// If this is true, an exception is being logged with no custom message
if (exception?.Message != null && !message.StartsWith(exception.Message))
{
// Exception won't be used as Breadcrumb message. Avoid losing it by adding as data:
data = new Dictionary<string, string>
{
{"exception_type", exception.GetType().ToString()},
{"exception_message", exception.Message},
};
}
if (IncludeEventDataOnBreadcrumbs)
{
if (shouldIncludeProperties)
{
var contextProps = GetAllProperties(logEvent);
data ??= new Dictionary<string, string>(contextProps.Count);
foreach (var contextProp in contextProps)
{
if (contextProp.Value?.ToString() is { } value)
{
data.Add(contextProp.Key, value);
}
}
}
}
hub.AddBreadcrumb(
_clock,
message,
breadcrumbCategory,
data: data,
level: logEvent.Level.ToBreadcrumbLevel());
}
}
private SentryUser? GetUser(LogEventInfo logEvent)
{
if (User is null)
{
return null;
}
var user = new SentryUser
{
Id = User.Id?.Render(logEvent),
Username = User.Username?.Render(logEvent),
Email = User.Email?.Render(logEvent),
IpAddress = User.IpAddress?.Render(logEvent),
#pragma warning disable CS0618 // Type or member is obsolete
Segment = User.Segment?.Render(logEvent)
#pragma warning restore CS0618 // Type or member is obsolete
};
if (User.Other?.Count > 0)
{
user.Other = new Dictionary<string, string>(User.Other.Count);
for (var i = 0; i < User.Other.Count; ++i)
{
if (User.Other[i].Layout?.Render(logEvent) is { } value)
{
if (!string.IsNullOrEmpty(value) || User.Other[i].IncludeEmptyValue)
{
user.Other[User.Other[i].Name] = value;
}
}
}
}
return user;
}
private IEnumerable<KeyValuePair<string, string>> GetTagsFromLogEvent(LogEventInfo logEvent)
{
if (IncludeEventPropertiesAsTags)
{
if (logEvent.HasProperties)
{
foreach (var kv in logEvent.Properties)
{
if (kv.Value?.ToString() is { } value)
{
yield return new KeyValuePair<string, string>(kv.Key?.ToString() ?? "", value);
}
}
}
}
if (Tags.Count > 0)
{
foreach (var tag in Tags)
{
var tagValue = RenderLogEvent(tag.Layout, logEvent);
if (!tag.IncludeEmptyValue && string.IsNullOrEmpty(tagValue))
{
continue;
}
yield return new KeyValuePair<string, string>(tag.Name, tagValue);
}
}
}
}