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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static IServiceCollection AddSentry<TOptions>(this IServiceCollection ser
services.TryAddSingleton<SentryOptions>(
c => c.GetRequiredService<IOptions<TOptions>>().Value);

// Must not capture a hub instance: https://github.com/getsentry/sentry-dotnet/issues/103
services.TryAddTransient<ISentryClient>(c => c.GetRequiredService<IHub>());
services.TryAddTransient(c => c.GetRequiredService<Func<IHub>>()());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,32 @@ public void Hub_CaptureMessage_EventCaptured()
));
}

[Fact]
public async Task UnhandledException_AnotherServiceProviderBuiltAndDisposed_EventCaptured()
{
IServiceCollection services = null;
ConfigureServices = s => services = s;
Build();

using (var throwaway = services.BuildServiceProvider())
{
_ = throwaway.GetRequiredService<IHub>();
}

_ = await HttpClient.GetAsync("/throw");

_ = Worker.Received(1).EnqueueEnvelope(Arg.Is<Envelope>(e =>
e.Items
.Select(i => i.Payload)
.OfType<JsonSerializable>()
.Select(i => i.Source)
.OfType<SentryEvent>()
.Single()
.Exception
.Message == "test error"
));
}

[Fact]
public async Task SendDefaultPii_FalseWithoutUserInRequest_NoUserNameSent()
{
Expand Down
6 changes: 6 additions & 0 deletions test/Sentry.Extensions.Logging.Tests/SentrySdkCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Sentry.Extensions.Logging.Tests;

[CollectionDefinition(nameof(SentrySdkCollection), DisableParallelization = true)]
public sealed class SentrySdkCollection
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Sentry.Extensions.Logging.Tests;

[Collection(nameof(SentrySdkCollection))]
public sealed class ServiceCollectionExtensionsTests : IDisposable
{
private readonly IBackgroundWorker _worker = Substitute.For<IBackgroundWorker>();

private ServiceProvider BuildServiceProvider()
{
var services = new ServiceCollection();
services.AddLogging(builder => builder.AddSentry(o =>
{
o.Dsn = ValidDsn;
o.BackgroundWorker = _worker;
o.InitNativeSdks = false;
}));
return services.BuildServiceProvider();
}

[Fact]
public void AddSentry_SdkHubReplacedAfterResolution_ResolvedHubUsesNewHub()
{
using var provider = BuildServiceProvider();
var hub = provider.GetRequiredService<IHub>();
var client = provider.GetRequiredService<ISentryClient>();

var newHub = Substitute.For<IHub>();
using var _ = SentrySdk.UseHub(newHub);

var hubEvent = new SentryEvent();
var clientEvent = new SentryEvent();
hub.CaptureEvent(hubEvent);
client.CaptureEvent(clientEvent);

newHub.Received(1).CaptureEvent(hubEvent);
newHub.Received(1).CaptureEvent(clientEvent);
}

[Fact]
public void AddSentry_ServiceProviderDisposed_SdkHubStillCaptures()
{
var provider = BuildServiceProvider();
_ = provider.GetRequiredService<IHub>();
_ = provider.GetRequiredService<ISentryClient>();
_ = provider.GetRequiredService<ILoggerFactory>();

provider.Dispose();
SentrySdk.CaptureMessage("after dispose");

_worker.Received(1).EnqueueEnvelope(Arg.Is<Envelope>(e =>
e.Items
.Select(i => i.Payload).OfType<JsonSerializable>()
.Select(i => i.Source).OfType<SentryEvent>()
.Any(evt => evt.Message!.Message == "after dispose")));
}

public void Dispose() => SentrySdk.Close();
}
Loading