Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion samples/Sentry.Samples.AspNetCore.Basic/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var builder = WebApplication.CreateBuilder(args);

builder.WebHost.UseSentry(options =>
builder.UseSentry(options =>
{
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
});
#endif

builder.WebHost.UseSentry(options =>
builder.UseSentry(options =>
{
options.Dsn = dsn;
#if NET10_0_OR_GREATER
Expand Down
2 changes: 1 addition & 1 deletion samples/Sentry.Samples.AspNetCore.Mvc/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// configuration system, which includes anything defined on the ConfigurationBuilder ('appsettings.json', environment
// variables etc.)
// See: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-8.0
builder.WebHost.UseSentry(options =>
builder.UseSentry(options =>
{
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, via the SENTRY_DSN environment variable or in your
Expand Down
4 changes: 2 additions & 2 deletions samples/Sentry.Samples.AspNetCore.Serilog/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public static WebApplication BuildWebApp(string[] args)
// or coded explicitly, via parameter like:
// .UseSentry("dsn") or .UseSentry(o => o.Dsn = ""; o.Release = "1.0"; ...)
#if !SENTRY_DSN_DEFINED_IN_ENV
builder.WebHost.UseSentry(SamplesShared.Dsn);
builder.UseSentry(SamplesShared.Dsn);
#else
builder.WebHost.UseSentry(EnvironmentVariables.Dsn);
builder.UseSentry(EnvironmentVariables.Dsn);
#endif

// The App:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var builder = WebApplication.CreateBuilder(args);

builder.WebHost.UseSentry(o =>
builder.UseSentry(o =>
{
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, in the SENTRY_DSN environment variable or in your appsettings.json
Expand Down
2 changes: 1 addition & 1 deletion samples/Sentry.Samples.GraphQL.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static WebApplication BuildWebApplication(string[] args)
.AddSentryOtlpExporter(dsn) // <-- Ensure telemetry is sent to Sentry
);

builder.WebHost.UseSentry(options =>
builder.UseSentry(options =>
{
options.Dsn = dsn;

Expand Down
2 changes: 1 addition & 1 deletion samples/Sentry.Samples.ME.AI.AspNetCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var builder = WebApplication.CreateBuilder(args);

builder.WebHost.UseSentry(options =>
builder.UseSentry(options =>
{
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
.AddSentryOtlpExporter(dsn)
);

builder.WebHost.UseSentry(options =>
builder.UseSentry(options =>
{
options.Dsn = dsn;
options.Debug = builder.Environment.IsDevelopment();
Expand Down
59 changes: 59 additions & 0 deletions src/Sentry.AspNetCore/SentryWebApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Microsoft.AspNetCore.Hosting;
using Sentry.AspNetCore;

// ReSharper disable once CheckNamespace
namespace Microsoft.AspNetCore.Builder;

/// <summary>
/// Extension methods to <see cref="WebApplicationBuilder"/>
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class SentryWebApplicationBuilderExtensions
{
/// <summary>
/// Uses Sentry integration.
/// </summary>
/// <param name="builder">The builder.</param>
public static WebApplicationBuilder UseSentry(this WebApplicationBuilder builder)
{
builder.WebHost.UseSentry();
return builder;
}

/// <summary>
/// Uses Sentry integration.
/// </summary>
/// <param name="builder">The builder.</param>
/// <param name="dsn">The DSN.</param>
public static WebApplicationBuilder UseSentry(this WebApplicationBuilder builder, string dsn)
{
builder.WebHost.UseSentry(dsn);
return builder;
}

/// <summary>
/// Uses Sentry integration.
/// </summary>
/// <param name="builder">The builder.</param>
/// <param name="configureOptions">The configure options.</param>
public static WebApplicationBuilder UseSentry(
this WebApplicationBuilder builder,
Action<SentryAspNetCoreOptions>? configureOptions)
{
builder.WebHost.UseSentry(configureOptions);
return builder;
}

/// <summary>
/// Uses Sentry integration.
/// </summary>
/// <param name="builder">The builder.</param>
/// <param name="configureSentry">The Sentry builder.</param>
public static WebApplicationBuilder UseSentry(
this WebApplicationBuilder builder,
Action<ISentryBuilder>? configureSentry)
{
builder.WebHost.UseSentry(configureSentry);
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
{
public static Microsoft.AspNetCore.Builder.IApplicationBuilder UseSentryTracing(this Microsoft.AspNetCore.Builder.IApplicationBuilder builder) { }
}
public static class SentryWebApplicationBuilderExtensions
{
public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder) { }
public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder, System.Action<Sentry.AspNetCore.ISentryBuilder>? configureSentry) { }
public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder, System.Action<Sentry.AspNetCore.SentryAspNetCoreOptions>? configureOptions) { }
public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder, string dsn) { }
}
}
namespace Microsoft.AspNetCore.Hosting
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
{
public static Microsoft.AspNetCore.Builder.IApplicationBuilder UseSentryTracing(this Microsoft.AspNetCore.Builder.IApplicationBuilder builder) { }
}
public static class SentryWebApplicationBuilderExtensions
{
public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder) { }
public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder, System.Action<Sentry.AspNetCore.ISentryBuilder>? configureSentry) { }
public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder, System.Action<Sentry.AspNetCore.SentryAspNetCoreOptions>? configureOptions) { }
public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder, string dsn) { }
}
}
namespace Microsoft.AspNetCore.Hosting
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
{
public static Microsoft.AspNetCore.Builder.IApplicationBuilder UseSentryTracing(this Microsoft.AspNetCore.Builder.IApplicationBuilder builder) { }
}
public static class SentryWebApplicationBuilderExtensions
{
public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder) { }
public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder, System.Action<Sentry.AspNetCore.ISentryBuilder>? configureSentry) { }
public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder, System.Action<Sentry.AspNetCore.SentryAspNetCoreOptions>? configureOptions) { }
public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder, string dsn) { }
}
}
namespace Microsoft.AspNetCore.Hosting
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Sentry.AspNetCore.Tests;

[Collection(nameof(SentrySdkCollection))]
public class SentryWebApplicationBuilderExtensionsTests : IDisposable
{
public void Dispose() => SentrySdk.Close();

private static WebApplicationBuilder CreateBuilder(
string environmentName = "Production",
params (string Key, string Value)[] settings)
{
var builder = WebApplication.CreateBuilder(new WebApplicationOptions { EnvironmentName = environmentName });
((IConfigurationBuilder)builder.Configuration).AddInMemoryCollection(
settings.ToDictionary(s => s.Key, s => s.Value));
return builder;
}

private static SentryAspNetCoreOptions ResolveOptions(WebApplicationBuilder builder)
{
using var services = builder.Services.BuildServiceProvider();
return services.GetRequiredService<IOptions<SentryAspNetCoreOptions>>().Value;
}

private static void DoNotSend(SentryAspNetCoreOptions options)
{
options.Dsn = ValidDsn;
options.BackgroundWorker = Substitute.For<IBackgroundWorker>();
options.AutoSessionTracking = false;
options.InitNativeSdks = false;
}

public static TheoryData<string, Func<WebApplicationBuilder, WebApplicationBuilder>> Overloads => new()
{
{ "parameterless", b => b.UseSentry() },
{ "dsn", b => b.UseSentry(ValidDsn) },
{ "options callback", b => b.UseSentry((SentryAspNetCoreOptions o) => DoNotSend(o)) },
{ "sentry builder callback", b => b.UseSentry((ISentryBuilder s) => s.AddSentryOptions(DoNotSend)) },
};

[Theory, MemberData(nameof(Overloads))]
public void UseSentry_AnyOverload_RegistersAspNetCoreIntegration(
string overload, Func<WebApplicationBuilder, WebApplicationBuilder> useSentry)
{
_ = overload;
var builder = CreateBuilder();

var returned = useSentry(builder);

returned.Should().BeSameAs(builder);
builder.Services.Should().ContainSingle(d => d.ServiceType == typeof(IHub));
builder.Services.Should().ContainSingle(d => d.ImplementationType == typeof(SentryStartupFilter));
builder.Services.Should().ContainSingle(d => d.ImplementationType == typeof(SentryTracingStartupFilter));
builder.Services.Should().ContainSingle(d => d.ImplementationType == typeof(SentryMiddleware));
builder.Services.Should().ContainSingle(d => d.ImplementationType == typeof(SentryAspNetCoreLoggerProvider));
builder.Services.Should().ContainSingle(d => d.ImplementationType == typeof(SentryAspNetCoreStructuredLoggerProvider));
}

[Fact]
public void UseSentry_SentryAndLoggingSections_BindsOptions()
{
var builder = CreateBuilder(settings: new[]
{
("Sentry:Dsn", ValidDsn),
("Sentry:SendDefaultPii", "true"),
("Logging:Sentry:MinimumEventLevel", "Warning"),
});

builder.UseSentry();

var options = ResolveOptions(builder);
options.Dsn.Should().Be(ValidDsn);
options.SendDefaultPii.Should().BeTrue();
options.MinimumEventLevel.Should().Be(LogLevel.Warning);
}

[Fact]
public void UseSentry_Dsn_SetsDsn()
{
var builder = CreateBuilder();

builder.UseSentry(ValidDsn);

ResolveOptions(builder).Dsn.Should().Be(ValidDsn);
}

[Fact]
public void UseSentry_SentryBuilderCallback_ConfiguresOptions()
{
var builder = CreateBuilder();

builder.UseSentry((ISentryBuilder sentryBuilder) =>
sentryBuilder.AddSentryOptions(options => options.SendDefaultPii = true));

ResolveOptions(builder).SendDefaultPii.Should().BeTrue();
}

[Fact]
public void UseSentry_OptionsCallback_RunsAfterConfigurationBinding()
{
var builder = CreateBuilder(settings: ("Sentry:SendDefaultPii", "true"));

builder.UseSentry(options => options.SendDefaultPii = false);

ResolveOptions(builder).SendDefaultPii.Should().BeFalse();
}

[Fact]
public void UseSentry_NoEnvironmentConfigured_UsesHostEnvironment()
{
var builder = CreateBuilder("Staging");

builder.UseSentry();

ResolveOptions(builder).Environment.Should().Be("staging");
}

[Fact]
public void UseSentry_AdjustStandardEnvironmentNameCasingInConfiguration_KeepsHostEnvironmentCasing()
{
var builder = CreateBuilder("Staging", ("Sentry:AdjustStandardEnvironmentNameCasing", "false"));

builder.UseSentry();

ResolveOptions(builder).Environment.Should().Be("Staging");
}

[Fact]
public void UseSentry_EnvironmentSetInCallback_KeepsEnvironment()
{
var builder = CreateBuilder("Staging");

builder.UseSentry(options => options.Environment = "from-code");

ResolveOptions(builder).Environment.Should().Be("from-code");
}

[Fact]
public async Task UseSentry_UnhandledRequestException_CapturedBySentryMiddleware()
{
var events = new List<SentryEvent>();
var builder = CreateBuilder();
builder.WebHost.UseTestServer();
builder.UseSentry(options =>
{
DoNotSend(options);
options.SetBeforeSend((e, _) =>
{
events.Add(e);
return null;
});
});
await using var app = builder.Build();
app.MapGet("/throw", string () => throw new InvalidOperationException("thrown from an endpoint"));
await app.StartAsync();

_ = await Record.ExceptionAsync(() => app.GetTestClient().GetAsync("/throw"));

events.Should().ContainSingle(e => e.Exception!.Message == "thrown from an endpoint");
}
}