Issue
It is impossible to configure Serilog Sinks from the command line Microsoft.AspNetCore.Mvc.Testing, or when certain command line arguments are set.
Expected Behavior
Logs are written to console in the requested form.
Actual Behavior
No logs are written!
How To Reproduce
- Create a new project using
dotnet new web
- Set the Program.cs to below
string[] sargs = [
"--Serilog=",
"--Serilog:WriteTo=",
"--Serilog:WriteTo:0=",
"--Serilog:WriteTo:0:Name=Console"
]
var builder = WebApplication.CreateBuilder
builder.Host.UseSerilog((host, services, configuration) =>
{
configuration.ReadFrom.Services(services);
configuration.ReadFrom.Configuration(host.Configuration);
});
var app = builder.Build();
app.Logger.LogInformation("test");
- Ensure there is no "Serilog" section in appsettings.json and appsettings..json
- Run the site via
dotnet run.
System / Version Information
- .NET SDK 8.0.415
- Windows 10 LTSC
Other Information
Using the simplified form, without setting the output template works as expected.
--Serilog:WriteTo:0=Console
Cause & Suggested Fix
ConfigurationReader.GetMethodCalls checks if the list contains strings or objects using Value!=null and Value==null checks.
This makes the assumption that any section with a value cannot be an object. However, that is incorrect. When entering setting values via Microsoft.AspNetCore.Mvc.Testing, all sections have a value set to "" instead of null.
Tabular representation of the difference:
| Section |
Value (Testing) |
Value (JSON) |
| Serilog |
"" |
null |
| Serilog:WriteTo |
"" |
null |
| Serilog:WriteTo:0 |
"" |
null |
| Serilog:WriteTo:0:Name |
"Console" |
"Console" |
Suggested Fix
Replace the null checks with string.IsNullOrEmpty
Edit: Adjusted root cause, and reproducible example.
Issue
It is impossible to configure Serilog Sinks from
the command lineMicrosoft.AspNetCore.Mvc.Testing, or when certain command line arguments are set.Expected Behavior
Logs are written to console in the requested form.
Actual Behavior
No logs are written!
How To Reproduce
dotnet new webdotnet run.System / Version Information
Other Information
Using the simplified form, without setting the output template works as expected.
Cause & Suggested Fix
ConfigurationReader.GetMethodCalls checks if the list contains strings or objects using
Value!=nullandValue==nullchecks.This makes the assumption that any section with a value cannot be an object. However, that is incorrect. When entering setting values via Microsoft.AspNetCore.Mvc.Testing, all sections have a value set to "" instead of null.
Tabular representation of the difference:
Suggested Fix
Replace the null checks with
string.IsNullOrEmptyEdit: Adjusted root cause, and reproducible example.