question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Settings aren't being picked up from appsettings.json when calling loggerFactory.AddSentry()

See original GitHub issue

I’m using Sentry in a .net core console app without the generic host. This is my basic setup though I have refactored it a bit as you’ll see below.

When I add Sentry to the LoggerFactory it’s not picking up the settings from my appsettings.json as expected. The following code is what’s currently working for me:

Program.cs

public static void Main(string[] args)
{
    SetupConfiguration(args);
    SetupServices();

    using (Services)
    using (var loggerFactory = Services.GetService<ILoggerFactory>())
    {
        // ...
    }
}

private static void SetupServices()
{
    var instrumentationKey = Configuration.GetValue<string>("APPINSIGHTS_INSTRUMENTATIONKEY");

    var loggerFactory = new LoggerFactory()
                        .AddConsole()
                        .AddSentry(options => Configuration.Bind("Sentry", options)); // This ensures the settings are loaded

    if (!string.IsNullOrEmpty(instrumentationKey))
    {
        loggerFactory.AddApplicationInsights(instrumentationKey, null);
    }

    var services = new ServiceCollection()
                   .AddSingleton(Configuration)
                   .AddSingleton(loggerFactory)
                   .AddLogging(config =>
                   {
                       //config.AddSentry();
                   });

    ConfigureServices(services);

    Services = services.BuildServiceProvider();
}

private static void SetupConfiguration(string[] args)
{
    var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

    var builder = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables();

    if (string.Equals(environment, "Development", StringComparison.OrdinalIgnoreCase))
    {
        builder.AddUserSecrets<Functions>();
    }

    if (args != null)
    {
        builder.AddCommandLine(args);
    }

    Configuration = builder.Build();
}

appsettings.json

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },

  "Sentry": {
    "Dsn": "https://xxxx@sentry.io/xxxx"
  }
}

I’ve tried moving the .AddSentry() call to inside .AddLogging() but I’m not able to get all of the loggers to resolve with a mixed configuration, so I need to stick to one method of setup for now. I did try loading just the console & sentry loggers inside .AddLogging() and the settings weren’t being picked up in there either.

The latest WebJobs SDK beta uses HostBuilder which is what asp.net core is built on and .net core console apps are meant to use. This will allow for a similar configuration setup to asp.net core. These builds haven’t been published to MyGet or NuGet so I haven’t tried them out yet.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
pengweiqhcacommented, Aug 30, 2018
new LoggerFactory(builder => builder.AddConfiguration(Configuration.GetSection("Logging"))
0reactions
bruno-garciacommented, Sep 7, 2018

Resolves on rc2. Please reopen if you found any issues

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP.NET Core web service does not load appsettings.json ...
WebHostBuilder doesn't load appsettings.json by default, you need to manually call AddJsonFile . For example: return new WebHostBuilder() .
Read more >
Logging in .NET Core and ASP.NET Core
Configure logging. Logging configuration is commonly provided by the Logging section of appsettings.{ENVIRONMENT}.json files, where the { ...
Read more >
Intro to AppSettings in .NET Core - YouTube
AppSettings are a big deal in .NET Core. If you are just using appsettings. json, you are really missing out. There is so...
Read more >
Using User Secrets Configuration In .NET - NET Core Tutorials
User Secrets follow the same paradigm as all other configuration in .NET. So if you are using an appsettings.json, Azure Keyvault, Environment Variables...
Read more >
NLog Tutorial - The essential guide for logging from C#
Learn about everything from logging and structured log messages to layout renderers and configuration of common targets.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found