serilog-sinks-console icon indicating copy to clipboard operation
serilog-sinks-console copied to clipboard

JSON outputTemplate in Serilog Console log not showing Timestamp

Open mtotaro opened this issue 1 year ago • 1 comments

Hi 👋,

I'm experiencing an issue with the outputTemplate in Serilog when trying to log JSON-formatted messages to the console. Instead of rendering the actual values, it outputs placeholder words such as {Timestamp:HH:mm:ss}, {Level:w3}, etc. I expected to see actual timestamps, severity levels, and messages in the log output.

Details:

Serilog Package ID and Version: Serilog 4.0.1 Toolchain, Framework, and OS: .NET 8 SDK on Windows 10. Configuration: Here’s a sample configuration for WriteTo in appsettings.json:

"WriteTo": [
    {
        "Name": "AmazonCloudWatch",
        "Args": {
            "logGroup": "test",
            "logStreamPrefix": "app",
            "region": "eu-west-1",
            "restrictedToMinimumLevel": "Information",
            "createLogGroup": true
        }
    },
    {
        "Name": "Console",
        "Args": {
            "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
            "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
            "outputTemplate": "{\"time\": \"{Timestamp:HH:mm:ss}\", \"severity\": \"{Level:w3}\", \"msg\": \"{Message:lj}\", \"exception\": \"{Exception}\", \"correlationID\": \"{TraceId}\" }{NewLine}"
        }
    }
]

Current Behavior:

The console output currently shows the following instead of the actual values:

{"time": "{Timestamp:HH:mm:ss}", "severity": "inf", "msg": "Request finished HTTP/2 GET https://localhost:7026/_vs/browserLink - 200 null text/javascript; charset=UTF-8 215.1437ms", "exception": "", "correlationID": "e08cdf60b8bb30c12f7ae2db8f592935" }}

As shown, placeholder such as {Timestamp:HH:mm:ss} is not being replaced with actual values.

Thanks in advance!

mtotaro avatar Nov 13 '24 00:11 mtotaro

Hi! The formatter and outputTemplate options aren't compatible; if you want to customize the JSON format you might best look at using formatter with ExpressionTemplate.

This post has recipes for constructing different JSON formats: https://nblumhardt.com/2021/06/customize-serilog-json-output/

And there's an example of the appsettings.json configuration syntax to use these in: https://github.com/serilog/serilog-settings-configuration?tab=readme-ov-file#complex-parameter-value-binding

HTH!

nblumhardt avatar Nov 13 '24 06:11 nblumhardt