-
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
Extension methods with IConfiguration parameter are ignored #142
Comments
Thanks for the report, sounds like something was overlooked 👍 |
I don't think it's a bug in the config package.. The SQL sink relies on this to find a named connectionstring in a section outside of the Serilog-specific section. Using the dev version of the SQL sink and the 3.0.0 release of the config package, this code and config works and is probably the simplest way to reproduce this without writing your own sink: var appConfig = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(path: "appsettings.json")
.Build();
var logConfig = new LoggerConfiguration();
logConfig.ReadFrom.Configuration(appConfig);
Log.Logger = logConfig.CreateLogger();
Serilog.Debugging.SelfLog.Enable(Console.Error);
Log.Information("An Information message");
Log.CloseAndFlush(); {
"ConnectionStrings": {
"DevTest": "Data Source=(LocalDB)\\MSSQLLocalDB;Initial Catalog=SerilogTest;Integrated Security=true"
},
"Serilog": {
"Using": [ "Serilog.Sinks.Console" ],
"WriteTo": [
{ "Name": "Console" },
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "DevTest",
"tableName": "Issue81",
"autoCreateSqlTable": true,
"columnOptionsSection": {
"customColumns": [
{
"ColumnName": "EventType",
"DataType": "int"
},
{
"ColumnName": "Release",
"DataType": "varchar",
"DataLength": 32
}
]
}
}
}
]
}
} |
@MV10 If you look at the source code (I guess you're the author) of that sink (configuration part), you'll see it assigns a default value So this proves what I wrote. |
@andriysavin The default value is irrelevant. It works because the SQL sink is able to resolve the named connection string, which is outside of the Serilog section. This means the full |
@MV10 correct, but if that parameter is not assigned default value, the method is not called. Try it yourself. |
@andriysavin Ah, I missed that part when I read your original mesage, sorry about that. Weird. I'll take a look. |
@andriysavin @MV10 I could reproduce that behavior (and fix it) as part of #144.
|
A fix should be available in the pre-release nuget package for v3.0.1 : https://www.nuget.org/packages/Serilog.Settings.Configuration/3.0.1-dev-00160 It would be great if you could give it a try ! |
Package v3.0.1 has now been released - https://www.nuget.org/packages/Serilog.Settings.Configuration/3.0.1 Thanks for your report ! |
Note that this turns out to be a breaking change for the code which used workaround with assigning default value. In this case the configuration parameter remains null (but the method is now called). For example, if SQL Server sink we discussed above upgrades this package to 3.0.1, I believe it will always get null in |
When you define sink configuration extension method with
IConfiguration
parameter that method is ignored. At the same time if you useIConfigurationSection
and specify value in config source, the method is discovered and the parameter is populated. Also, if default value (null) is specified forIConfiguration
parameter all works as exepected as well. I believe the source of the problem lies in the code which filters methods and doesn't take into accountIConfiguration
parameter type:serilog-settings-configuration/src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs
Line 351 in 51a4fca
The text was updated successfully, but these errors were encountered: