Skip to content

Null values are stringified differently than string.Format #1620

@chtenb

Description

@chtenb

Description
The documentation states that

Message templates are a superset of standard .NET format strings, so any format string acceptable to string.Format() will also be correctly processed by Serilog.

However, null values are printed by serilog as null but by string.Format as "". If this is intentional the docs should reflect this and my question becomes: how do I stringify null values as an empty string?

Reproduction

using Serilog;
using System.IO;

namespace ConsoleApp1 {
  class Program {
    static void Main(string[] args) {
      var logFileDir = @"path\to\dir";

      Log.Logger = new LoggerConfiguration().WriteTo.File(Path.Combine(logFileDir, "all.log")).CreateLogger();

      long? v = null;
      Log.Error("A long value {Value}.", v);
      Log.Information($"A long value {v}.");
      Log.CloseAndFlush();
    }
  }
}

The output is

2021-09-02 17:49:31.186 +02:00 [ERR] A long value null.
2021-09-02 17:49:31.206 +02:00 [INF] A long value .

Expected behavior

2021-09-02 17:49:31.186 +02:00 [ERR] A long value .
2021-09-02 17:49:31.206 +02:00 [INF] A long value .

Relevant package, tooling and runtime versions
Windows 10

  <package id="Serilog" version="2.10.0" targetFramework="net48" />
  <package id="Serilog.Sinks.File" version="5.0.0" targetFramework="net48" />

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