Skip to content

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

Closed

Description

@jnoordsij

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.

Activity

  1. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Jan 8, 2025
  2. jamescrosswell commented on Jan 9, 2025

    @jamescrosswell
    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"];
    });
  3. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Jan 9, 2025
  4. added
    .NETPull requests that update .net code
    on Aug 6, 2025
  5. meriturva commented on Sep 23, 2025

    @meriturva

    Here my workaround:

    webBuilder.UseSentry(options =>
    {
        options.Dsn = options.Dsn ?? string.Empty;
    });
    
  6. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Sep 23, 2025
  7. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Sep 24, 2025
  8. 14 remaining items

  9. jnoordsij commented on Sep 10, 2026

    @jnoordsij
    Author

    From my initial tests on .NET v11.0.0-rc.1.26425.128, which includes dotnet/runtime@d6089ff, this indeed seems fixed.

  10. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Sep 10, 2026
  11. ric-oliv commented on Oct 19, 2026

    @ric-oliv
    Member

    Thank you for your report (and support) here, @jnoordsij and @meriturva !

    In the .NET SDK version 7.0 (with #5739) we're introducing a new Sentry.Extensions.Hosting package with UseSentry specific for generic host apps. It reads the Sentry section of appsettings.json the same way Sentry.AspNetCore currently does, so "Dsn": "" disables the SDK.

    var builder = Host.CreateApplicationBuilder(args);
    builder.UseSentry();
    {
      "Sentry": {
        "Dsn": ""
      }
    }

    Host.CreateDefaultBuilder(args).UseSentry() works the same way. The Sentry.Samples.GenericHost sample will now also use UseSentry.

    One thing for the upgrade: in 7.0, Logging.AddSentry() no longer initializes the SDK (#5595), so the setup in your ConfigureLogging moves to UseSentry.

  12. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Oct 19, 2026
  13. self-assigned this
    on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

.NETPull requests that update .net codeBugSomething isn't workingLogs

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions