-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
ApplicationBuilderExtensions.cs
67 lines (61 loc) · 2.63 KB
/
ApplicationBuilderExtensions.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
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
#if NETSTANDARD2_0
using IHostApplicationLifetime = Microsoft.AspNetCore.Hosting.IApplicationLifetime;
#else
using Microsoft.Extensions.Hosting;
#endif
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Sentry;
using Sentry.AspNetCore;
using Sentry.Extensibility;
using Sentry.Extensions.Logging;
using Sentry.Infrastructure;
// ReSharper disable once CheckNamespace -- Discoverability
namespace Microsoft.AspNetCore.Builder
{
/// <summary>
/// Extension methods for <see cref="IApplicationBuilder"/>
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
internal static class ApplicationBuilderExtensions
{
/// <summary>
/// Use Sentry integration
/// </summary>
/// <param name="app">The application.</param>
/// <returns></returns>
public static IApplicationBuilder UseSentry(this IApplicationBuilder app)
{
// Container is built so resolve a logger and modify the SDK internal logger
var options = app.ApplicationServices.GetService<IOptions<SentryAspNetCoreOptions>>();
if (options?.Value is { } o)
{
if (o.Debug && (o.DiagnosticLogger == null || o.DiagnosticLogger.GetType() == typeof(ConsoleDiagnosticLogger)))
{
var logger = app.ApplicationServices.GetRequiredService<ILogger<ISentryClient>>();
o.DiagnosticLogger = new MelDiagnosticLogger(logger, o.DiagnosticLevel);
}
var stackTraceFactory = app.ApplicationServices.GetService<ISentryStackTraceFactory>();
if (stackTraceFactory != null)
{
o.UseStackTraceFactory(stackTraceFactory);
}
if (app.ApplicationServices.GetService<IEnumerable<ISentryEventProcessor>>()?.Any() == true)
{
o.AddEventProcessorProvider(app.ApplicationServices.GetServices<ISentryEventProcessor>);
}
if (app.ApplicationServices.GetService<IEnumerable<ISentryEventExceptionProcessor>>()?.Any() == true)
{
o.AddExceptionProcessorProvider(app.ApplicationServices.GetServices<ISentryEventExceptionProcessor>);
}
}
var lifetime = app.ApplicationServices.GetService<IHostApplicationLifetime>();
lifetime?.ApplicationStopped.Register(SentrySdk.Close);
return app.UseMiddleware<SentryMiddleware>();
}
}
}