Settings aren't being picked up from appsettings.json when calling loggerFactory.AddSentry()
See original GitHub issueI’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:
- Created 5 years ago
- Comments:6 (6 by maintainers)
Top GitHub Comments
Resolves on rc2. Please reopen if you found any issues