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

Cannot pass empty string as Dsn from appsettings.json when using generic host builder #3875

Open
jnoordsij opened this issue Jan 8, 2025 · 1 comment

Comments

@jnoordsij
Copy link

jnoordsij commented Jan 8, 2025

Package

Sentry

.NET Flavor

.NET

.NET Version

9.0.0

OS

Android

SDK Version

9.0.101

Self-Hosted Sentry Version

No response

Steps to Reproduce

Run the Sentry.Samples.GenericHost project with "Dsn": "" set in appsettings.json

Expected Result

The application to run with Sentry SDK disabled

Actual Result

System.ArgumentNullException: 'Value cannot be null. (Parameter 'You must supply a DSN to use Sentry.To disable Sentry, pass an empty string: "".See https://docs.sentry.io/platforms/dotnet/configuration/options/#dsn')'

Further details

This seems to affect only setups with generic host builder; with web host setting an empty string in appsettings.json works just fine.

In my setup the call is located within a IHostBuilder.ConfigureLogging, which suffers from this issue as it is similar to the sample above and thus most likely caused by same underlying reason.

My own research showed that dotnet/runtime#62532 and/or dotnet/runtime#65594 might be the root cause of this issue, as the .NET runtime seems to cast empty strings to null in some particular situations. If possible, it would be nice to have a way to prevent this from happening within the scope of the Sentry SDK.

Workaround

When manually setting the DSN with e.g.

builder.Logging.AddSentry((sentryOptions) =>
{
    sentryOptions.Dsn = builder.Configuration["Sentry:Dsn"];
});

the problem does not occur.

@jamescrosswell
Copy link
Collaborator

Thanks for the sleuthing @jnoordsij. 🙏🏻

I added my 2c to the issue in the dotnet/runtime repo.

As a workaround, you could do something like this when initialising Sentry in your generic host:

var builder = Host.CreateApplicationBuilder();

builder.Logging.AddConfiguration(builder.Configuration);
builder.Logging.AddSentry(options =>
{
    options.Dsn = builder.Configuration["Sentry:Dsn"];
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Status: No status
Development

No branches or pull requests

2 participants