Skip to content
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

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.ComponentModel;
using System.Linq;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Sentry.AspNetCore;
using Sentry.Extensibility;
Expand All @@ -20,17 +22,55 @@ internal static class ServiceCollectionExtensions
/// <returns></returns>
public static ISentryBuilder AddSentry(this IServiceCollection services)
{
services.AddSingleton<ISentryEventProcessor, AspNetCoreEventProcessor>();
if(!services.IsRegistered<ISentryEventProcessor, AspNetCoreEventProcessor>())
{
services.AddSingleton<ISentryEventProcessor, AspNetCoreEventProcessor>();
}

services.TryAddSingleton<IUserFactory, DefaultUserFactory>();

services
.AddSingleton<IRequestPayloadExtractor, FormRequestPayloadExtractor>()
// Last
.AddSingleton<IRequestPayloadExtractor, DefaultRequestPayloadExtractor>();

if(!services.IsRegistered<IRequestPayloadExtractor, FormRequestPayloadExtractor>())
{
services.AddSingleton<IRequestPayloadExtractor, FormRequestPayloadExtractor>();
}

// Last
if(!services.IsRegistered<IRequestPayloadExtractor, DefaultRequestPayloadExtractor>())
{
services.AddSingleton<IRequestPayloadExtractor, DefaultRequestPayloadExtractor>();
}

services.AddSentry<SentryAspNetCoreOptions>();

return new SentryAspNetCoreBuilder(services);
}

/// <summary>
/// Checks if a specific ServiceDescriptor was previously registered in the <see cref="IServiceCollection"/>
/// </summary>
/// <param name="serviceCollection"></param>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TU"></typeparam>
/// <returns></returns>
internal static bool IsRegistered<T, TU>(this IServiceCollection serviceCollection)
{
return serviceCollection.Any(x => x.ImplementationType == typeof(T) && x.ServiceType == typeof(TU));
}

/// <summary>
/// Adds Sentry's StartupFilter to the <see cref="IServiceCollection"/>
/// </summary>
/// <param name="serviceCollection"></param>
/// <returns></returns>
internal static IServiceCollection AddSentryStartupFilter(this IServiceCollection serviceCollection)
{
if(!serviceCollection.IsRegistered<IStartupFilter, SentryStartupFilter>())
Copy link
Member

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?

Suggested change
if(!serviceCollection.IsRegistered<IStartupFilter, SentryStartupFilter>())
if (!serviceCollection.IsRegistered<IStartupFilter, SentryStartupFilter>())

{
_ = serviceCollection.AddTransient<IStartupFilter, SentryStartupFilter>();
}

return serviceCollection;
}
}
}
1 change: 1 addition & 0 deletions src/Sentry.AspNetCore/Sentry.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.Abstractions" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.1.0" />
Expand Down
93 changes: 93 additions & 0 deletions src/Sentry.AspNetCore/SentryHostBuilderExtensions.cs
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IHostBuilder lives in Microsoft.Extensions.Hosting so this could be added to Sentry.Extensions.Logging.

Copy link
Author

Choose a reason for hiding this comment

The 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.
Would you like me to move it ?

=> 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;
}
}
}
41 changes: 41 additions & 0 deletions src/Sentry.AspNetCore/SentryLoggingBuilderExtensions.cs
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();
}
}
}
23 changes: 5 additions & 18 deletions src/Sentry.AspNetCore/SentryWebHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.ComponentModel;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Options;
using Sentry.AspNetCore;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -81,24 +78,14 @@ public static IWebHostBuilder UseSentry(
// In case the logging backend was replaced, init happens later, at the StartupFilter
_ = builder.ConfigureLogging((context, logging) =>
{
logging.AddConfiguration();

var section = context.Configuration.GetSection("Sentry");
_ = logging.Services.Configure<SentryAspNetCoreOptions>(section);

_ = logging.Services
.AddSingleton<IConfigureOptions<SentryAspNetCoreOptions>, SentryAspNetCoreOptionsSetup>();
_ = logging.Services.AddSingleton<ILoggerProvider, SentryAspNetCoreLoggerProvider>();

_ = logging.AddFilter<SentryAspNetCoreLoggerProvider>(
"Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware",
LogLevel.None);

var sentryBuilder = logging.Services.AddSentry();
var sentryBuilder = logging.AddSentry(context.Configuration);
configureSentry?.Invoke(context, sentryBuilder);
});

_ = builder.ConfigureServices(c => _ = c.AddTransient<IStartupFilter, SentryStartupFilter>());
_ = builder.ConfigureServices(c =>
{
_ = c.AddSentryStartupFilter();
});

return builder;
}
Expand Down