Skip to content

[Bug] Serilog Sinks Cannot Be Configured From Microsoft.AspNetCore.Mvc.Testing #459

@EmperorArthur

Description

@EmperorArthur

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

  1. Create a new project using dotnet new web
  2. 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");
  1. Ensure there is no "Serilog" section in appsettings.json and appsettings..json
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions