Yair Maron
Yair Maron

Reputation: 1958

How can I include both "Message" and "MessageTemplate" in the Serilog JsonFormatter output?

I'm using Serlog 2.10, Writing to Console (as human-readable text), and to RollingFile - with JsonFormatter.

I want in the output JSON to have both: "Message" and "MessageTemplate" nodes,

However I'm getting only either "Message" or "MessageTemplate", but not both.

Here's how it's being configured:

Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Information()
                .WriteTo.Console()
                .WriteTo.RollingFile(new JsonFormatter(), "logs/log-{Date}.log")
                .CreateLogger();

I followed the instructions on this page: https://github.com/serilog/serilog-formatting-compact

I've tried all 3 Json Formatters:

How can I include both of them in the output?

Any assistance would be appreciated

Upvotes: 8

Views: 4510

Answers (3)

Pikabanga
Pikabanga

Reputation: 85

There is an optional parameter renderMessage to the JsonFormatter constructor that adds RenderedMessage to the json:

JsonFormatter

Upvotes: 4

Nicholas Blumhardt
Nicholas Blumhardt

Reputation: 31832

Serilog.Expressions can do this:

  .WriteTo.RollingFile(
    new ExpressionTemplate(
      "{ {Timestamp: @t, Level: @l, MessageTemplate: @mt, Message: @m, Exception: @x, " +
      "Properties: if IsDefined(@p[?]) then @p else undefined()} }" + Environment.NewLine,
      "logs/log-{Date}.log")

Upvotes: 2

FantasticFiasco
FantasticFiasco

Reputation: 1193

One way is to write your own formatter. Start from e.g. the JsonFormatter and only use those parts that are valid for your requirements.

Upvotes: 2

Related Questions