Skip to content

A bad designed Class can cause an Exception when using Stringify with JsonFormatter or JsonValueFormatter #1451

@rafaelsc

Description

@rafaelsc

Description
A bad designed Class can cause an Exception when logging with stringification operator. Using the JsonFormatter or a Formatter that uses the JsonValueFormatter internally like CompactJsonFormatter or RenderedCompactJsonFormatter.

Reproduction
Log using stringification operator with a instance of a class that can return null in ToString(). With configured to use JsonFormatter or CompactJsonFormatter or RenderedCompactJsonFormatter.

	Log.Logger = new LoggerConfiguration()
		.WriteTo.Console(new Serilog.Formatting.Json.JsonFormatter()) //Or other JsonFormatter
		.CreateLogger();

        class ABadBehavior
        {
            public override string ToString() => null;
        }
        Log.Information("{$Data}", new ABadBehavior());

Expected behavior
That the Serilog Core doesn't throw an exception. And Log the result like was another null property.

Relevant package, tooling and runtime versions
All runtimes and using the lastest Serilog codebase.

Additional context
I find this issue when implementing the C#8 Nullable Reference Type in Serilog codebase.
Related Issue #1426

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions