Skip to content

Commit bfc23f0

Browse files
authored
Merge pull request #160 from getsentry/feature/aspnetcore-configure-scope
feat: UseSentry:ConfigureScope
2 parents 4035e25 + 8f4b5ba commit bfc23f0

File tree

8 files changed

+71
-0
lines changed

8 files changed

+71
-0
lines changed

samples/Sentry.Samples.AspNetCore.Mvc/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Net;
33
using Microsoft.AspNetCore;
44
using Microsoft.AspNetCore.Hosting;
5+
using Sentry;
56

67
namespace Samples.AspNetCore.Mvc
78
{
@@ -39,6 +40,9 @@ public static IWebHost BuildWebHost(string[] args) =>
3940

4041
options.MaxQueueItems = 100;
4142
options.ShutdownTimeout = TimeSpan.FromSeconds(5);
43+
44+
// Configures the root scope
45+
options.ConfigureScope(s => s.SetTag("Always sent", "this tag"));
4246
})
4347
.Build();
4448
}

samples/Sentry.Samples.ME.Logging/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using Microsoft.Extensions.Logging;
4+
using Sentry;
45
using Sentry.Extensions.Logging;
56
using Sentry.Protocol;
67

@@ -28,6 +29,8 @@ private static void Main()
2829
// Don't keep as a breadcrumb or send events for messages of level less than Critical with exception of type DivideByZeroException
2930
o.AddLogEntryFilter((category, level, eventId, exception)
3031
=> level < LogLevel.Critical && exception?.GetType() == typeof(DivideByZeroException));
32+
33+
o.ConfigureScope(s => s.SetTag("RootScope", "sent with all events"));
3134
}))
3235
{
3336
var logger = loggerFactory.CreateLogger<Program>();

src/Sentry.AspNetCore/SentryMiddleware.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public SentryMiddleware(
5151
_next = next ?? throw new ArgumentNullException(nameof(next));
5252
_hubAccessor = hubAccessor ?? throw new ArgumentNullException(nameof(hubAccessor));
5353
_options = options?.Value;
54+
if (_options != null)
55+
{
56+
var hub = _hubAccessor();
57+
foreach (var callback in _options.ConfigureScopeCallbacks)
58+
{
59+
hub.ConfigureScope(callback);
60+
}
61+
}
5462
_hostingEnvironment = hostingEnvironment;
5563
_logger = logger;
5664
}

src/Sentry.Extensions.Logging/SentryLoggerFactoryExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public static ILoggerFactory AddSentry(
5050
hub = HubAdapter.Instance;
5151
}
5252

53+
foreach (var callback in options.ConfigureScopeCallbacks)
54+
{
55+
hub.ConfigureScope(callback);
56+
}
57+
5358
factory.AddProvider(new SentryLoggerProvider(hub, SystemClock.Clock, options));
5459
return factory;
5560
}

src/Sentry.Extensions.Logging/SentryLoggingOptions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Immutable;
23
using Microsoft.Extensions.Logging;
34
using Sentry.Protocol;
@@ -50,9 +51,20 @@ public class SentryLoggingOptions : SentryOptions
5051
/// </summary>
5152
public bool InitializeSdk { get; set; } = true;
5253

54+
/// <summary>
55+
/// Add a callback to configure the scope upon SDK initialization
56+
/// </summary>
57+
/// <param name="action">The function to invoke when initializing the SDK</param>
58+
public void ConfigureScope(Action<Scope> action) => ConfigureScopeCallbacks = ConfigureScopeCallbacks.Add(action);
59+
5360
/// <summary>
5461
/// Log entry filters
5562
/// </summary>
5663
internal ImmutableList<ILogEntryFilter> Filters { get; set; } = ImmutableList<ILogEntryFilter>.Empty;
64+
65+
/// <summary>
66+
/// List of callbacks to be invoked when initializing the SDK
67+
/// </summary>
68+
internal ImmutableList<Action<Scope>> ConfigureScopeCallbacks { get; set; } = ImmutableList<Action<Scope>>.Empty;
5769
}
5870
}

test/Sentry.AspNetCore.Tests/MiddlewareLoggerIntegration.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Sentry.Extensions.Logging;
1010
using Sentry.Infrastructure;
1111
using Sentry.Internal;
12+
using Sentry.Protocol;
1213
using Xunit;
1314

1415
namespace Sentry.AspNetCore.Tests
@@ -102,6 +103,21 @@ public async Task InvokeAsync_LoggerPushesScope_LoggerMessage_AsBreadcrumb()
102103
Arg.Is<Scope>(e => e.Breadcrumbs.Any(b => b.Message == expectedCrumb)));
103104
}
104105

106+
[Fact]
107+
public async Task InvokeAsync_OptionsConfigureScope_AffectsAllRequests()
108+
{
109+
const SentryLevel expected = SentryLevel.Debug;
110+
_fixture.Options.ConfigureScope(s => s.Level = expected);
111+
_fixture.RequestDelegate = context => throw new Exception();
112+
var sut = _fixture.GetSut();
113+
114+
await Assert.ThrowsAsync<Exception>(async () => await sut.InvokeAsync(_fixture.HttpContext));
115+
116+
_fixture.Client.Received(1).CaptureEvent(
117+
Arg.Any<SentryEvent>(),
118+
Arg.Is<Scope>(e => e.Level == expected));
119+
}
120+
105121
public void Dispose() => _fixture.Dispose();
106122
}
107123
}

test/Sentry.Extensions.Logging.Tests/SentryLoggerFactoryExtensionsTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
1+
using System;
12
using Microsoft.Extensions.Logging;
23
using NSubstitute;
34
using Sentry.Extensibility;
45
using Sentry.Internal;
6+
using Sentry.Protocol;
57
using Xunit;
68

79
namespace Sentry.Extensions.Logging.Tests
810
{
911
public class SentryLoggerFactoryExtensionsTests
1012
{
13+
[Fact]
14+
public void AddSentry_ConfigureScope_InvokesCallback()
15+
{
16+
const SentryLevel expected = SentryLevel.Debug;
17+
var sut = Substitute.For<ILoggerFactory>();
18+
var hub = Substitute.For<IHub>();
19+
var scope = new Scope(new SentryOptions());
20+
hub.When(w => w.ConfigureScope(Arg.Any<Action<Scope>>()))
21+
.Do(info => info.Arg<Action<Scope>>()(scope));
22+
SentrySdk.UseHub(hub);
23+
24+
sut.AddSentry(o =>
25+
{
26+
o.InitializeSdk = false; // use the mock above
27+
o.ConfigureScope(s => s.Level = expected);
28+
});
29+
30+
Assert.Equal(expected, scope.Level);
31+
}
32+
1133
[Fact]
1234
public void AddSentry_InitializeSdkFalse_HubAdapter()
1335
{

test/Sentry.Extensions.Logging.Tests/SentryLoggerProviderTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using NSubstitute;
33
using Sentry.Infrastructure;
4+
using Sentry.Protocol;
45
using Xunit;
56

67
namespace Sentry.Extensions.Logging.Tests

0 commit comments

Comments
 (0)