【问题标题】:Nlog with .NET core- How to log JSON object without the messageNlog with .NET core - 如何在没有消息的情况下记录 JSON 对象
【发布时间】:2019-07-18 00:50:26
【问题描述】:

我正在使用带有 .NET 核心和依赖注入的 Nlog 来记录 JSON 对象。 我的日志目前看起来像:{"log":{"rersourceId": "2"}} {"log":{"rersourceId": "423"}},我希望我的日志打印为没有消息/属性名称字段的简单 JSON 对象:{"rersourceId": "2"}{"rersourceId": "423"}

这是我的 Nlog.config:

    <?xml version="1.0" encoding="utf-8" ?>
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="info"
      internalLogFile="C:\\Data\\LogFiles\\ScheduleVisits\\logging_api\\NLog.log">

  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>

  </extensions>
  <variable name="LogBaseFolder" value="C:\\Data\\LogFiles\\ScheduleVisits\\logging_api" />
  <targets>



    <target name="ScheduleVisitsLogAsyncWrapper"
            xsi:Type="AsyncWrapper"
            batchSize="200"
            queueLimit="200000"
            timeToSleepBetweenBatches="0">

      <target
        name="ScheduleVisitsLog"
        xsi:type="File"
        fileName="${LogBaseFolder}\\${uppercase:${level}}\\${shortdate}.json"
        archiveAboveSize="10485760"
        archiveEvery="Day"
        createDirs="True"
        encoding="utf-8"
        keepFileOpen="false"
        archiveFileName ="${LogBaseFolder}\\Archive\\${uppercase:${level}}\\ScheduleVisitsAll_{#####}.zip"
        archiveNumbering="DateAndSequence"
        enableArchiveFileCompression="True">
        <layout xsi:type="JsonLayout" includeAllProperties="True" suppressSpaces="True">
          <attribute name="properties" layout="${json-event-properties}" encode="false"/>
        </layout>
      </target>
    </target>
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <logger name="*"
        minlevel="Trace"
        writeTo="ScheduleVisitsLogAsyncWrapper">
      <filters>
        <when condition="starts-with('${logger}','Microsoft.')" action="Ignore" />
      </filters>
    </logger>

  </rules>
</nlog>

在我的 Program.cs 中,我在“Main”方法中添加了这些行:

var factory = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config");
NLog.Config.ConfigurationItemFactory.Default.JsonConverter = new JsonNetSerializer();
var logger = factory.GetCurrentClassLogger();

我的 appSettings.json 文件:

{

  "Environment": 1,
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Trace",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

这就是我记录属性的方式

_logger.LogInformation("{log}", input);

我必须添加一条消息作为LogInformation 的第一个参数,并且我想摆脱它。

【问题讨论】:

标签: c# dependency-injection .net-core nlog


【解决方案1】:

尝试将占位符从 {log} 重命名为 {@resourceId} 并记录该属性:(不要忘记 @ 符号)

_logger.LogInformation("{@resourceId}{@anotherResourceId}", input.YourResourceIdProperty, input.AnotherResourceIdProperty);

我不确定,但您可能还需要从您的 nlog.config 文件中删除以下行:

<attribute name="properties" layout="${json-event-properties}" encode="false"/>

您还可以在此处的类似帖子中查看我的答案: How to log message without even properties

【讨论】:

    【解决方案2】:

    ${json-event-properties} 是什么?

    您是否尝试过像这样启用maxRecursionLimit="10"

        <layout xsi:type="JsonLayout" includeAllProperties="True" suppressSpaces="True" maxRecursionLimit="10">
        </layout>
    

    或者像这样:

        <layout xsi:type="JsonLayout" suppressSpaces="True">
               <attribute name="properties" encode="false">
                   <layout xsi:type="JsonLayout" includeAllProperties="True" suppressSpaces="True" maxRecursionLimit="10" />
               </attribute>
        </layout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-12
      • 2019-04-13
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 2013-04-05
      相关资源
      最近更新 更多