NLog JSON layout doesn't render nested properties if value is null

31 Views Asked by At

I'm trying to log structured data with Nlog (5.2.8) and using event-properties.

enter image description here

The NLog Config:

<target name="jsonFile" xsi:type="File"
fileName="C:\Logs\JSON.log"
archiveFileName="C:\Logs\Archive\xxx_${shortdate}_JSON.log"
archiveEvery="Hour"
maxArchiveFiles="48"
archiveDateFormat="yyyy-MM-dd HH_mm_ss">
    <layout xsi:type="JsonLayout" excludeEmptyProperties="false" renderEmptyObject="true">
        <attribute name='time' layout='${longdate}' />
        <attribute name='level' layout='${level:upperCase=true}'/>
        <attribute name="logger" layout='${logger}' />
        <attribute name="host" layout="${machinename}" />
        <attribute name="Service" layout="${gdc:item=service}" />
        <attribute name='exception' layout='${exception}' />
        <attribute name="eventProperties" encode="false">
            <layout type='JsonLayout' includeEventProperties="true" maxRecursionLimit="3" excludeEmptyProperties="false" renderEmptyObject="true"/>
        </attribute>
        <attribute name="Scoped" encode="false">
            <layout type='JsonLayout' includeMdc="true" maxRecursionLimit="10"/>
        </attribute>
        <attribute name="gdc" encode="false" >
            <layout type='JsonLayout' includeGdc="true"  maxRecursionLimit="2"/>
        </attribute>
    </layout>
</target>

If I have a property on top level, the null values was rendered as 'NULL' as expected.

If I have properies in nested JSON object with null values, I cant get it rendered as you can see in the picture.

The JSON output as text:

{
    "time": "2024-01-31 18:53:17.2573",
    "level": "INFO",
    "logger": "Ifi-Logger",
    "host": "xx118",
    "message": "Template Testfunktion for 'LogTestTemplateFunc' ...",
    "eventProperties": {
        "NullString": null,
        "Test_func_1": {
            "Name": "Waldi",
            "Alter": 12,
            "Tier": "Hund",
            "Parasit": {
                "Name": "Floh",
                "Anzahl": 24
            }
        },
        "Test_func_2": {
            "Alter": 2,
            "Tier": "Fisch",
            "Parasit": {
                "Anzahl": 1
            }
        }
    }
}

In object Test_func_2 the 'Name' fields are set to null and disapears from layout!

Are there any restriction or automatism to render null values in nested objects?

Thanks for any hints

I have tried everything and what I expected was a output like this:

{
    "time": "2024-01-31 18:53:17.2573",
    "level": "INFO",
    "logger": "Ifi-Logger",
    "host": "xx118",
    "message": "Template Testfunktion for 'LogTestTemplateFunc' ...",
    "eventProperties": {
        "NullString": null,
        "Test_func_1": {
            "Name": "Waldi",
            "Alter": 12,
            "Tier": "Hund",
            "Parasit": {
                "Name": "Floh",
                "Anzahl": 24
            }
        },
        "Test_func_2": {
            "Name": null,
            "Alter": 2,
            "Tier": "Fisch",
            "Parasit": {
                "Name": null,
                "Anzahl": 1
            }
        }
    }
}
0

There are 0 best solutions below