Reputation: 1958
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
Reputation: 85
There is an optional parameter renderMessage
to the JsonFormatter constructor that adds RenderedMessage
to the json:
Upvotes: 4
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
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