-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Configuration with decimal-point numbers #325
Comments
👍 Here's where the conversion currently happens: Unfortunately, Microsoft.Extensions.Configuration doesn't internally support numbers - all configuration values are essentially strings by the time they reach any Serilog code. I think a fix for this will require opt-in to avoid pernicious breakages in existing apps; e.g. |
Adding an optional |
I have started working on this issue on my FormatProvider branch. But I also have pull request #347 which is currently in progress. This pull request already adds a parameter to the Also, adding parameters to the @nblumhardt: Would you consider adding an optional |
Also, while we are talking about potential breaking changes, I'd argue that the default format provider should be changed from Why? Because the Full sample code with stack tracevar json = "{ \"number\": 0.1 }"u8;
using var jsonStream = new MemoryStream(json.ToArray());
var configuration = new ConfigurationBuilder().AddJsonStream(jsonStream).Build();
{ "number": 0.1 } And since JSON numbers are written with a decimal point ( Note that the same argument also holds if using TOML instead of JSON, for example through the Tomlyn.Extensions.Configuration NuGet package. |
Hi @0xced! I agree that Regarding added optional args, I think given the role of this package, it'd be reasonable to break binary compatibility (it's rare for sinks to depend on it, and they're by far the biggest concern with respect to binary compat). One of my hopes for the Serilog 3.0 wave is that the various Serilog.Extensions.* packages switch their major/minor versioning strategy to match 1:1 with the target Microsoft.Extensions.* packages; this would make the new version 7.0.x I guess 🤔 |
If we are talking about break binary compatibility, then I propose to group the entire set of additional parameters in the settings class. This will allow to protect code from further breaking changes when adding new settings in the future: public static LoggerConfiguration Configuration(
this LoggerSettingsConfiguration settingConfiguration,
IConfiguration configuration,
LoggerConfigurationOptions options = null)
{
options ??= new();
// go on with values from options instance
}
public class LoggerConfigurationOptions
{
public string SectionName { get; set; } = DefaultSectionName;
public CultureInfo Culture { get; set; } = CultureInfo.InvariantCulture;
// and so on
} So only one method exists, no combinations now and in the future, no breaking changes also (in ideal). |
Also introduce a new `Options` class to avoid `ReadFrom.Configuration()` exponential number of methods explosion when adding new options. Fixes serilog#325
Also introduce a new `Options` class to avoid `ReadFrom.Configuration()` exponential number of methods explosion when adding new options. Fixes serilog#325
Also introduce a new `Options` class to avoid `ReadFrom.Configuration()` exponential number of methods explosion when adding new options. Fixes serilog#325
Also introduce a new `Options` class to avoid `ReadFrom.Configuration()` exponential number of methods explosion when adding new options. Fixes serilog#325
Also introduce a new `Options` class to avoid `ReadFrom.Configuration()` methods exponential growth when adding new options. All _older_ `Configuration()` methods go through the newly introduced `Configuration(LoggerSettingsConfiguration, IConfiguration, Options)` method that takes an `Options` instance. Older methods explicitly set the `FormatProvider` option to `null` in order to preserve backward compatbility. By using the new `Configuration()` method, users opt into the new default of having the invariant culture as the format provider. Note: the `= null` default value in the `Configuration()` method taking a `DependencyContext` has been removed in order to make sure the CS0121 compilation does not occur: > [CS0121] The call is ambiguous between the following methods or properties: 'ConfigurationLoggerConfigurationExtensions.Configuration(LoggerSettingsConfiguration, IConfiguration, DependencyContext)' and 'ConfigurationLoggerConfigurationExtensions.Configuration(LoggerSettingsConfiguration, IConfiguration, Options)' Fixes serilog#325
Also introduce a new `ConfigurationContext` class to avoid `ReadFrom.Configuration()` methods exponential growth when adding new options. All _older_ `Configuration()` methods go through the newly introduced `Configuration(LoggerSettingsConfiguration, IConfiguration, ConfigurationContext)` method that takes an `ConfigurationContext` instance. Older methods explicitly set the `FormatProvider` option to `null` in order to preserve backward compatibility. By using the new `Configuration()` method, users opt into the new default of having the invariant culture as the format provider. Note: the `= null` default value in the `Configuration()` method taking a `DependencyContext` has been removed in order to make sure the CS0121 compilation does not occur: > [CS0121] The call is ambiguous between the following methods or properties: 'ConfigurationLoggerConfigurationExtensions.Configuration(LoggerSettingsConfiguration, IConfiguration, DependencyContext)' and 'ConfigurationLoggerConfigurationExtensions.Configuration(LoggerSettingsConfiguration, IConfiguration, ConfigurationContext)' Fixes serilog#325
Also introduce a new `ConfigurationContext` class to avoid `ReadFrom.Configuration()` methods exponential growth when adding new options. All _older_ `Configuration()` methods go through the newly introduced `Configuration(LoggerSettingsConfiguration, IConfiguration, ConfigurationContext)` method that takes an `ConfigurationContext` instance. Older methods explicitly set the `FormatProvider` option to `null` in order to preserve backward compatibility. By using the new `Configuration()` method, users opt into the new default of having the invariant culture as the format provider. Note: the `= null` default value in the `Configuration()` method taking a `DependencyContext` has been removed in order to make sure the CS0121 compilation does not occur: > [CS0121] The call is ambiguous between the following methods or properties: 'ConfigurationLoggerConfigurationExtensions.Configuration(LoggerSettingsConfiguration, IConfiguration, DependencyContext)' and 'ConfigurationLoggerConfigurationExtensions.Configuration(LoggerSettingsConfiguration, IConfiguration, ConfigurationContext)' Fixes serilog#325
Also introduce a new `ConfigurationReaderOptions` class to avoid `ReadFrom.Configuration()` methods exponential growth when adding new options. All _older_ `Configuration()` methods go through the newly introduced `Configuration(LoggerSettingsConfiguration, IConfiguration, ConfigurationReaderOptions)` method that takes an `ConfigurationReaderOptions` instance. Older methods explicitly set the `FormatProvider` option to `null` in order to preserve backward compatibility. By using the new `Configuration()` method, users opt into the new default of having the invariant culture as the format provider. Note: the `= null` default value in the `Configuration()` method taking a `DependencyContext` has been removed in order to make sure the CS0121 compilation does not occur: > [CS0121] The call is ambiguous between the following methods or properties: 'ConfigurationLoggerConfigurationExtensions.Configuration(LoggerSettingsConfiguration, IConfiguration, DependencyContext)' and 'ConfigurationLoggerConfigurationExtensions.Configuration(LoggerSettingsConfiguration, IConfiguration, ConfigurationReaderOptions)' Fixes serilog#325
When reading configuration for sinks that include decimal-point numbers, the configuration reads the value as a string and then parses it using whatever number format the current culture supports.
Example of configuring Sentry as a sink, where SampleRate should be set to 10%::
The value is read / parsed as a string here:
argumentValue = new StringArgumentValue(argumentSection.Value);
While applying the configuration the rSampleRate value then depends on whether the current context supports point or comma as decimal point, giving either 0.1 (10%) or 1 (100%) as result.
The desired behavior would be for the number value to be parsed as a number and not a string, when the value is supplied as a number.
Package versions used:
The text was updated successfully, but these errors were encountered: