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 1 commit
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;
}
}
}
24 changes: 5 additions & 19 deletions src/Sentry.AspNetCore/SentryHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using System.ComponentModel;
using Microsoft.AspNetCore.Hosting;
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 @@ -82,24 +78,14 @@ public static IHostBuilder UseSentry(
// 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
{
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, s) => _ = s.AddTransient<IStartupFilter, SentryStartupFilter>());
_ = builder.ConfigureServices((c, s) =>
{
_ = s.AddSentryStartupFilter();
});

return builder;
}
Expand Down
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