Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON outputTemplate in Serilog Console log not showing Timestamp #163

Open
mtotaro opened this issue Nov 13, 2024 · 1 comment
Open

JSON outputTemplate in Serilog Console log not showing Timestamp #163

mtotaro opened this issue Nov 13, 2024 · 1 comment
Labels

Comments

@mtotaro
Copy link

mtotaro commented Nov 13, 2024

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!

@nblumhardt
Copy link
Member

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants