Reputation: 1
I'm trying to use IOptionsMonitor to change ILogger log level at runtime in appsettings.json, but despite that I have used IOptionsMonitor for other settings like email settings used in application, in this case it returns old or default values..
I can change the values, I can open appsettings.json to see that values are changed, but loading values from appsettings.json returns, I guess, default values.. Anyone had this issue before?
IOptionsMonitor mapping class:
public class LoggingSettings
{
public const string SectionName = "Logging";
public LogLevelSettings LogLevel { get; set; }
}
public class LogLevelSettings
{
public const string SectionName = "LogLevel";
[ConfigurationKeyName(name: "Default")]
public string DefaultSetting { get; set; }
[ConfigurationKeyName(name: "Microsoft.AspNetCore")]
public string MicrosoftAspNetCore { get; set; }
}
section in appsettings.json:
"Logging": {
"LogLevel": {
"Default": "Critical",
"Microsoft.AspNetCore": "None"
}
}
excerpt from LogSettingsModel.cshtml.cs constructor (page):
public LogSettingsModel(IOptionsMonitor<LoggingSettings> logSettings)
{
//this returns values "Information" and "Warning" for "Default" and "Microsoft.AspNetCore" keys instead "Critical" and "None"
_logSettings = logSettings.CurrentValue;
//this also returns "Information" for "Default" key, configurationHelper is static class made for accessing options..
var sec = ConfigurationHelper.config.GetSection("Logging:LogLevel:Default");
}
ConfigurationHelper class (created if I need to access config from static methods and for testing):
public static class ConfigurationHelper
{
public static IConfiguration config;
public static IWebHostEnvironment env;
public static void Initialize(IConfiguration configuration, IWebHostEnvironment environment)
{
config = configuration;
env = environment;
}
}
I have noticed that issue seems to happend because all other settings are loaded from "appsetings.json" (when running locally) - but this specific values are (I believe) loaded from "appsettings.Development.json", not sure why this is happening. So, if I manually change LogLevel:Default to "None" in appsettings.json, it still will be loaded with setting "Information".
When inspecting with debugger breakpoint set at LogSettingsModel.cshtml.cs, I can see that there are settings in appsettings.Development.json.
It seems that when appsettings.Development.json has some values inside, that values are loaded instead appsettings.json values.
Upvotes: 0
Views: 766
Reputation: 149
appsettings.development.json is not "hidden". IConfiguration is key-value dictionary. A value for a key provided by latter configuration provider overwrites preceding value. By default appsettings.Development.json
is loaded after appsettings.json
. So, if a key exists in both files, values from appsettings.json
are overwritten. That's all.
Just one remark: appsettings.Development.json is loaded in Development environment only.
Upvotes: 1
Reputation: 1
I forgot that appsettings.development.json is "hidden" under appsettings.json.. It seems that appsettings.Development.json (in development locally) has priority over appsettings.json and values from appsettings.json are overriden with values from appsettings.Development.json.. Thanks to all participants for their time!
Upvotes: 0