getsentry / getsentry/sentry-dotnet

Use DI in Sentry.Extensions.Logging to register ISentryEventProcessor

Open
#361 2 comments 1 reaction 0 assignees View on GitHub
.NET Feature Microsoft.Extensions.Logging
Dominant language
C#
Stars
770
Forks
248
Avg merge
3d 4h
Merged PRs (30d)
49

Description

Please mark the type framework used: ASP.NET MVC

Please mark the type of the runtime used: .NET Framework

Please mark the NuGet packages used: Sentry.Extensions.Logging Version: 2.0.3

I can't find a way to leverage the use of ISentryEventProcessor and ISentryEventExceptionProcessor with Sentry.Extensions.Logging package.

I know I can instantiate it and register it this way at the beginning when I .AddSentry to ILoggingBuilder:

```c#
loggingBuilder.AddSentry(opt =>
{
sentrySection.Bind(opt);

// Here...
opt.AddEventProcessor(new MyEventProcessor());
opt.AddEventProcessorProvider(() => new[] { new MyEventProcessor() });

opt.ConfigureScope(scope =>
{
// ... or here
opt.AddEventProcessor(new MyEventProcessor());
opt.AddEventProcessorProvider(() => new[] { new MyEventProcessor() });
});
});
```

But I would prefer to use DI the same way you did in Sentry.AspNetCore:

```c#
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient();
services.AddScoped();
}
```

I'm almost sure that this won't work in Sentry.Extensions.Logging because the implementation of ILoggerProvider in that package does not use anything there except IHub:

```c#
///
/// Creates a new instance of .
///
/// The Options.
/// The Hub.
public SentryLoggerProvider(IOptions options, IHub hub)
: this(hub,
SystemClock.Clock,
options.Value)
{ }

internal SentryLoggerProvider(
IHub hub,
ISystemClock clock,
SentryLoggingOptions options)
{
Debug.Assert(options != null);
Debug.Assert(clock != null);
Debug.Assert(hub != null);

_disposableHub = hub as IDisposable;

Hub = hub;
_clock = clock;
_options = options;

if (hub.IsEnabled)
{
_scope = hub.PushScope();
hub.ConfigureScope(s =>
{
s.Sdk.Name = Constants.SdkName;
s.Sdk.Version = NameAndVersion.Version;
s.Sdk.AddPackage(ProtocolPackageName, NameAndVersion.Version);
});

// Add scope configuration to hub from options
foreach (var callback in options.ConfigureScopeCallbacks)
{
hub.ConfigureScope(callback);
}
}
}
```

What if you add `IEnumerable` and `IEnumerable` to the constructor to leverage any registered processor then add them to the options?

What is your thought on that feature? I know it will probably affects Sentry.AspNetCore...

I can work on this and create a PR if you guys interested?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.