简体   繁体   English

如何在 Serilog JsonFormatter output 中同时包含“Message”和“MessageTemplate”?

[英]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.我正在使用 Serlog 2.10、写入控制台(作为人类可读文本)和 RollingFile - 使用 JsonFormatter。

I want in the output JSON to have both: "Message" and "MessageTemplate" nodes,我希望在 output JSON 中同时拥有:“Message”和“MessageTemplate”节点,

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我按照此页面上的说明进行操作: https://github.com/serilog/serilog-formatting-compact

I've tried all 3 Json Formatters:我已经尝试了所有 3 个 Json 格式化程序:

  • "RenderedCompactJsonFormatter" - puts "@m" (Message) in the output “RenderedCompactJsonFormatter” - 将“@m”(消息)放入 output
  • "CompactJsonFormatter" - puts "@mt" (MessageTemlate) in the output “CompactJsonFormatter” - 将“@mt”(MessageTemplate)放入 output
  • "JsonFormatter" - puts "MessageTemplate" in the output “JsonFormatter” - 将“MessageTemplate”放入 output

How can I include both of them in the output?如何将它们都包含在 output 中?

Any assistance would be appreciated任何援助将不胜感激

One way is to write your own formatter.一种方法是编写自己的格式化程序。 Start from eg the JsonFormatter and only use those parts that are valid for your requirements.从例如JsonFormatter开始,只使用那些对您的要求有效的部分。

Serilog.Expressions can do this: Serilog.Expressions可以这样做:

  .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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM