【问题标题】:How to log message without even properties如何在没有属性的情况下记录消息
【发布时间】:2019-09-06 13:42:39
【问题描述】:

我正在使用 Nlog v4.6.7 并使用以下布局(在 Nlog.config 中)呈现消息。

 <layout xsi:type="JsonLayout" includeAllProperties="true">
      <attribute name="timestamp" layout="${date:universalTime=true:format=yyyy-MM-dd HH\:mm\:ss,fff}" />
      <attribute name="level" layout="${level}"/>
      <attribute name="message" layout="${message}" />
 </layout>

典型的日志记录是_logger.Info("Start {job} with {@data}", job, new {a,b,c});

我使用includeAllProperties 选项,因为每条消息可能定义不同的属性,我不能将它们一一作为属性预先包含在布局中。

上面打印出来的结果是这样的:

{ "timestamp": "2019-09-06 13:13:40,386", "level": "Info", "message": "Start \"SomeJobType\" with {\"a\":\"aa\", \"b\":\"bb\", \"c\":\"cc\"}", "job": "SomeJobType", "data": { "a": "aa", "b": "bb", "c": "cc" } }

有没有办法解除从事件属性打印的消息?因此, 实现类似

{ "timestamp": "2019-09-06 13:13:40,386", "level": "Info", "message": "Start action", "job": "SomeJobType", "data": { "a": "aa", "b": "bb", "c": "cc" } }

${message:raw=true} 没有帮助,因为它会打印像

这样的占位符

{ "timestamp": "2019-09-06 13:13:40,386", "level": "Info", "message": "Start {job} with {@data}", "job": "SomeJobType", "data": { "a": "aa", "b": "bb", "c": "cc" } }

【问题讨论】:

  • 你试过创建一个loggerEvent吗?!有了它,您可以编辑属性并根据需要处理它们。
  • 您可以使用${message:raw=true},而不是使用${message}。另见github.com/NLog/NLog/wiki/…
  • @RolfKristensen 感谢您的提示,但正如我在消息中所写的那样,此解决方案不是我正在寻找的解决方案,因为 if 会打印占位符。
  • @PedroBrito 实际上,我的代码最终记录了一个接受 MessageTemplateParameter lsit 的 NLog.LogEventInfo。我一开始并没有使用它,因为我想知道是否有更多的书本解决方案。不过,我想我会试一试。
  • MessageTemplateParameter-constructor 是一种特殊优化,用于与 Microsoft Extension Logging 中的消息解析器集成。考虑只使用LogEventInfo.Properties(请参阅下面的答案)

标签: c# nlog


【解决方案1】:

你总是可以这样做:

var logger = NLog.LogManager.GetCurrentClassLogger();
var theEvent = new NLog.LogEventInfo(NLog.LogLevel.Info, null, "Start action");
theEvent.Properties["job"] = job;
theEvent.Properties["data"] = new {a,b,c};
logger.Log(theEvent);

然后在JsonLayout上配置MaxRecursionLimit=1

 <layout xsi:type="JsonLayout" includeAllProperties="true" maxRecursionLimit="1">
      <attribute name="timestamp" layout="${date:universalTime=true:format=yyyy-MM-dd HH\:mm\:ss,fff}" />
      <attribute name="level" layout="${level}"/>
      <attribute name="message" layout="${message}" />
 </layout>

另见:https://github.com/NLog/NLog/wiki/EventProperties-Layout-Renderer

【讨论】:

    【解决方案2】:

    从您的nlog.config 文件中删除以下行:

    <attribute name="message" layout="${message}" />
    

    然后你可以打印任何你想要的对象,就像这样:

    _logger.LogInformation("{@myAnonymous}{@myOtherObject}", new 
    { 
       prop1 = "abc", 
       prop2 = 123,
       nested = 
       {
          nestedProp = true
       }
    }, myAnyTypeOfObject);
    

    您的日志输出将如下所示(JSON 美化视图):

    {
        "myAnonymous": {
            "prop1 ": "abc",
            "prop2": 123,
            "nested": {
                "nestedProp": true
            }
        },
        "myOtherObject": /*JSON representation of myAnyTypeOfObject object.*/
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-07-18
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      • 2010-10-28
      • 1970-01-01
      • 2023-04-06
      相关资源
      最近更新 更多