Giter Club home page Giter Club logo

Comments (9)

elfico avatar elfico commented on June 3, 2024 2

Curious... I'm not able to reproduce this. Do you think you could provide a small example program that demonstrates this issue (e.g. as a GitHub repo)?

Sorry for the late reply. I will do a minimal reproduction and send it.

from sentry-dotnet.

jamescrosswell avatar jamescrosswell commented on June 3, 2024

Are you using Serilog by any chance?

from sentry-dotnet.

elfico avatar elfico commented on June 3, 2024

Are you using Serilog by any chance?

No, I am using NLog.

This only started when I upgraded to 4.0.0.

If I revert back to a lower version, it starts working fine.

from sentry-dotnet.

jamescrosswell avatar jamescrosswell commented on June 3, 2024

Curious... I'm not able to reproduce this. Do you think you could provide a small example program that demonstrates this issue (e.g. as a GitHub repo)?

from sentry-dotnet.

jamescrosswell avatar jamescrosswell commented on June 3, 2024

Here's the Minimal API that I tested (binding configuration to appsettings.json and using NLog for logging):

using NLog;
using NLog.Config;
using Sentry;

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseSentry(); 

LogManager.Configuration = new LoggingConfiguration();
LogManager.Configuration
    .AddSentry(o =>
    {
        // Optionally specify a separate format for message
        o.Layout = "${message}";
        // Optionally specify a separate format for breadcrumbs
        o.BreadcrumbLayout = "${logger}: ${message}";

        // Debug and higher are stored as breadcrumbs (default is Info)
        o.MinimumBreadcrumbLevel = NLog.LogLevel.Debug;
        // Error and higher is sent as event (default is Error)
        o.MinimumEventLevel = NLog.LogLevel.Info;

        // Send the logger name as a tag
        o.AddTag("logger", "${logger}");

        // All Sentry Options are accessible here.
    });

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

var summaries = new[]
{
    "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecast", () =>
    {
        SentrySdk.CaptureMessage("Retrieving the weather forecast");
        var forecast = Enumerable.Range(1, 5).Select(index =>
                new WeatherForecast
                (
                    DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                    Random.Shared.Next(-20, 55),
                    summaries[Random.Shared.Next(summaries.Length)]
                ))
            .ToArray();
        app.Logger.LogInformation("Retrieved weather forecast: returning to client");
        return forecast;
    })
    .WithName("GetWeatherForecast")
    .WithOpenApi();

app.Run();

record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

from sentry-dotnet.

Silvenga avatar Silvenga commented on June 3, 2024

Might be related to #3134 - but in our case, we want to not configure Sentry when no DSN is specified.

from sentry-dotnet.

jamescrosswell avatar jamescrosswell commented on June 3, 2024

Might be related to #3134 - but in our case, we want to not configure Sentry when no DSN is specified.

Possibly related. If you were having issues with the config bindings more generally, that would explain why you ended up with a null DSN (even though you'd supplied it as an environment variable). If there's any way you can provide a small sample project that reproduces this, that would really help. So far I haven't been able to reproduce the issue @elfico reported.

from sentry-dotnet.

elfico avatar elfico commented on June 3, 2024

@jamescrosswell
Here is a repo that shows a minimal working example: https://github.com/elfico/sentry-extension-example

from sentry-dotnet.

jamescrosswell avatar jamescrosswell commented on June 3, 2024

@jamescrosswell Here is a repo that shows a minimal working example: https://github.com/elfico/sentry-extension-example

Hey @elfico , thanks for the repo.

I noticed in your Startup class you have the following to initialize the SDK:

            //initialize sentry sdk
            SentrySdk.Init(sentry =>
            {
                SentryOptions options = new SentryOptions();

                // Add this to the SDK initialization callback
                // To set a uniform sample rate
                options.TracesSampleRate = 1.0;

So none of the options you're configuring there are being used for anything (that options variable passes out of scope and gets garbage collected at the end of the callback).

You wouldn't generally new up any options in the callback - rather the options get passed in as a parameter... so that code should look like this instead:

            //initialize sentry sdk
            SentrySdk.Init(options =>
            {
                // Add this to the SDK initialization callback
                // To set a uniform sample rate
                options.TracesSampleRate = 1.0;

Also, if you're initializing via SentrySdk.Init (which you'd typically do in a Console app) then you don't get configuration bindings out of the box... meaning none of the config from your appsettings.json file will be used. You could wire it up to work but it's a bit unusual.

Instead, I'd just move all of that initialization from your Startup class to Program.CreateHostBuilder (you're calling UseSentry there and that's where you do get configuration bindings).

This fixes the problem. The reason you were getting the null DSN error is that you were calling SentrySdk.Init without any DSN (and since this method doesn't read the configuration from your appsettings.json, it was throwing an exception).

from sentry-dotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.