-
-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
48 lines (40 loc) · 1.67 KB
/
ServiceCollectionExtensions.cs
File metadata and controls
48 lines (40 loc) · 1.67 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
using System.ComponentModel;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Http;
using Microsoft.Extensions.Options;
using Sentry.Extensibility;
namespace Sentry.Extensions.Logging.Extensions.DependencyInjection;
/// <summary>
/// Extension methods for <see cref="IServiceCollection"/>
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds Sentry's services to the <see cref="IServiceCollection"/>
/// </summary>
/// <param name="services">The services.</param>
public static IServiceCollection AddSentry<TOptions>(this IServiceCollection services)
where TOptions : SentryLoggingOptions, new()
{
services.TryAddSingleton<SentryOptions>(
c => c.GetRequiredService<IOptions<TOptions>>().Value);
services.TryAddTransient<ISentryClient>(c => c.GetRequiredService<IHub>());
services.TryAddTransient(c => c.GetRequiredService<Func<IHub>>()());
services.TryAddSingleton<Func<IHub>>(c =>
{
var options = c.GetRequiredService<IOptions<TOptions>>().Value;
if (options.InitializeSdk)
{
var hub = SentrySdk.InitHub(options);
SentrySdk.UseHub(hub);
}
return () => HubAdapter.Instance;
});
// Custom handler for HttpClientFactory.
// Must be singleton: https://github.com/getsentry/sentry-dotnet/issues/785
services.TryAddSingleton<IHttpMessageHandlerBuilderFilter, SentryHttpMessageHandlerBuilderFilter>();
return services;
}
}