-
-
Notifications
You must be signed in to change notification settings - Fork 211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add IHostBuilder support #1015
Add IHostBuilder support #1015
Changes from 6 commits
7c052dd
efe7497
79e1ab5
7726e9a
ee2f2c2
8e518a2
7095a47
fc84525
8d3d3de
7fd6cd2
6638eca
06ca369
459325b
824e856
d3bc651
1bfce46
ded818b
f3dc8d4
cd4ea2f
885ace1
42a84ba
7c20f88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Sentry.AspNetCore; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.Extensions.Hosting | ||
{ | ||
/// <summary> | ||
/// Extension methods to <see cref="IHostBuilder"/> | ||
/// </summary> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public static class SentryHostBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Uses Sentry integration. | ||
/// </summary> | ||
/// <param name="builder">The builder.</param> | ||
/// <returns></returns> | ||
public static IHostBuilder UseSentry(this IHostBuilder builder) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would've personally kept it in the asp.net core part, as it is the replacement of WebHostBuilder in the future. |
||
=> builder.UseSentry((Action<SentryAspNetCoreOptions>?)null); | ||
|
||
/// <summary> | ||
/// Uses Sentry integration. | ||
/// </summary> | ||
/// <param name="builder">The builder.</param> | ||
/// <param name="dsn">The DSN.</param> | ||
/// <returns></returns> | ||
public static IHostBuilder UseSentry(this IHostBuilder builder, string dsn) | ||
=> builder.UseSentry(o => o.Dsn = dsn); | ||
|
||
/// <summary> | ||
/// Uses Sentry integration. | ||
/// </summary> | ||
/// <param name="builder">The builder.</param> | ||
/// <param name="configureOptions">The configure options.</param> | ||
/// <returns></returns> | ||
public static IHostBuilder UseSentry( | ||
this IHostBuilder builder, | ||
Action<SentryAspNetCoreOptions>? configureOptions) | ||
=> builder.UseSentry((_, options) => configureOptions?.Invoke(options)); | ||
|
||
/// <summary> | ||
/// Uses Sentry integration. | ||
/// </summary> | ||
/// <param name="builder">The builder.</param> | ||
/// <param name="configureOptions">The configure options.</param> | ||
/// <returns></returns> | ||
public static IHostBuilder UseSentry( | ||
this IHostBuilder builder, | ||
Action<HostBuilderContext, SentryAspNetCoreOptions>? configureOptions) | ||
=> builder.UseSentry((context, sentryBuilder) => | ||
sentryBuilder.AddSentryOptions(options => configureOptions?.Invoke(context, options))); | ||
|
||
/// <summary> | ||
/// Uses Sentry integration. | ||
/// </summary> | ||
/// <param name="builder">The builder.</param> | ||
/// <param name="configureSentry">The Sentry builder.</param> | ||
/// <returns></returns> | ||
public static IHostBuilder UseSentry( | ||
this IHostBuilder builder, | ||
Action<ISentryBuilder>? configureSentry) => | ||
builder.UseSentry((_, sentryBuilder) => configureSentry?.Invoke(sentryBuilder)); | ||
|
||
/// <summary> | ||
/// Uses Sentry integration. | ||
/// </summary> | ||
/// <param name="builder">The builder.</param> | ||
/// <param name="configureSentry">The Sentry builder.</param> | ||
/// <returns></returns> | ||
public static IHostBuilder UseSentry( | ||
this IHostBuilder builder, | ||
Action<HostBuilderContext, ISentryBuilder>? configureSentry) | ||
{ | ||
// The earliest we can hook the SDK initialization code with the framework | ||
// Initialization happens at a later time depending if the default MEL backend is enabled or not. | ||
// In case the logging backend was replaced, init happens later, at the StartupFilter | ||
_ = builder.ConfigureLogging((context, logging) => | ||
bruno-garcia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var sentryBuilder = logging.AddSentry(context.Configuration); | ||
configureSentry?.Invoke(context, sentryBuilder); | ||
}); | ||
|
||
_ = builder.ConfigureServices((c, s) => | ||
{ | ||
_ = s.AddSentryStartupFilter(); | ||
}); | ||
|
||
return builder; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.ComponentModel; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Configuration; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Sentry.AspNetCore | ||
{ | ||
/// <summary> | ||
/// Extension methods for <see cref="ILoggingBuilder"/> | ||
/// </summary> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
internal static class SentryLoggingBuilderExtensions | ||
{ | ||
public static ISentryBuilder AddSentry(this ILoggingBuilder builder, IConfiguration configuration) | ||
{ | ||
builder.AddConfiguration(); | ||
|
||
var section = configuration.GetSection("Sentry"); | ||
_ = builder.Services.Configure<SentryAspNetCoreOptions>(section); | ||
|
||
if(!builder.Services.IsRegistered<IConfigureOptions<SentryAspNetCoreOptions>, SentryAspNetCoreOptionsSetup>()) | ||
{ | ||
_ = builder.Services | ||
.AddSingleton<IConfigureOptions<SentryAspNetCoreOptions>, SentryAspNetCoreOptionsSetup>(); | ||
} | ||
|
||
if(!builder.Services.IsRegistered<ILoggerProvider, SentryAspNetCoreLoggerProvider>()) | ||
{ | ||
_ = builder.Services.AddSingleton<ILoggerProvider, SentryAspNetCoreLoggerProvider>(); | ||
} | ||
|
||
_ = builder.AddFilter<SentryAspNetCoreLoggerProvider>( | ||
"Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware", | ||
LogLevel.None); | ||
|
||
return builder.Services.AddSentry(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: one day we'll have a linter that reformats and pushed to the branch :)
Could you please do this throughout the change?