简体   繁体   中英

Nlog with .NET core- How to log JSON object without the message

I am using Nlog with .NET core and dependency injection to log JSON objects. my logs currently look like : {"log":{"rersourceId": "2"}} {"log":{"rersourceId": "423"}} and I want my logs to be printed as simple JSON objects without the message/property name field : {"rersourceId": "2"}{"rersourceId": "423"}

Here is my 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>

In my Program.cs I added these lines in the 'Main' method:

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

my appSettings.json file:

{

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

and this is how I log the properties

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

I must add a message as the first argument to LogInformation and I want to get rid of it.

Try to rename the placeholder from {log} to {@resourceId} and log that property: (don't forget @ sign)

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

I am not sure but you may also need to remove the line below from your nlog.config file:

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

Also you can have a look to my answer at a similar post here: How to log message without even properties

What is ${json-event-properties} ?

Have you tried to enable maxRecursionLimit="10" like this:

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

Or like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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