-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathLoggingBuilderExtensions.cs
More file actions
72 lines (62 loc) · 2.97 KB
/
LoggingBuilderExtensions.cs
File metadata and controls
72 lines (62 loc) · 2.97 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
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Options;
using Sentry.Extensions.Logging;
using Sentry.Extensions.Logging.Extensions.DependencyInjection;
// ReSharper disable once CheckNamespace
// Ensures 'AddSentry' can be found without: 'using Sentry;'
namespace Microsoft.Extensions.Logging;
/// <summary>
/// LoggingBuilder extensions.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class LoggingBuilderExtensions
{
/// <summary>
/// Adds the Sentry logging integration.
/// </summary>
/// <param name="builder">The builder.</param>
public static ILoggingBuilder AddSentry(this ILoggingBuilder builder)
=> builder.AddSentry((Action<SentryLoggingOptions>?)null);
/// <summary>
/// Adds the Sentry logging integration.
/// </summary>
/// <param name="builder">The builder.</param>
/// <param name="dsn">The DSN.</param>
public static ILoggingBuilder AddSentry(this ILoggingBuilder builder, string dsn)
=> builder.AddSentry(o => o.Dsn = dsn);
/// <summary>
/// Adds the Sentry logging integration.
/// </summary>
/// <param name="builder">The builder.</param>
/// <param name="optionsConfiguration">The options configuration.</param>
public static ILoggingBuilder AddSentry(this ILoggingBuilder builder, Action<SentryLoggingOptions>? optionsConfiguration)
=> builder.AddSentry<SentryLoggingOptions>(optionsConfiguration);
internal static ILoggingBuilder AddSentry<TOptions>(
this ILoggingBuilder builder,
Action<TOptions>? optionsConfiguration)
where TOptions : SentryLoggingOptions, new()
{
builder.AddConfiguration();
if (optionsConfiguration != null)
{
builder.Services.Configure(optionsConfiguration);
}
builder.Services.AddSingleton<IConfigureOptions<TOptions>, SentryLoggingOptionsSetup>();
builder.Services.AddSingleton<ILoggerProvider, SentryLoggerProvider>();
builder.Services.AddSingleton<ILoggerProvider, SentryStructuredLoggerProvider>();
builder.Services.AddSentry<TOptions>();
// All logs should flow to the SentryLogger, regardless of level.
// Filtering of events is handled in SentryLogger, using SentryOptions.MinimumEventLevel
// Filtering of breadcrumbs is handled in SentryLogger, using SentryOptions.MinimumBreadcrumbLevel
builder.AddFilter<SentryLoggerProvider>(_ => true);
// Logs from the SentryLogger should not flow to the SentryStructuredLogger as this may cause recursive invocations.
// Filtering of logs is handled in SentryStructuredLogger, using SentryOptions.MinimumLogLevel
builder.AddFilter<SentryStructuredLoggerProvider>(static (string? categoryName, LogLevel logLevel) =>
{
return categoryName is null
|| categoryName != "Sentry.ISentryClient";
});
return builder;
}
}