1

So I am using NLog and the JSON Layout Target to log my .Net Application and its data which is so far working mostly fine.

However I have an issue where some of my objects contain Byte Arrays of large FileData that get included in the log file output which for file size reasons I don't want to include.

Looking at the NLOG tutorials available there is an "excludeProperties" attribute that I can include in the Layout Configuration however I can't seem to get this to actually work specifically.

So If my Object looks like,

 public class Station : IStation
        {
       
            public int ID { get; set; }
            public string Name { get; set; }
            public string MACAddress { get; set; }
            public string ComputerName { get; set; }
    }

and I use the following Layout Config in NLog,

 <layout type='JsonLayout'>
                <attribute name='Time' layout='${longdate}' />
                <attribute name='Level' layout='${level:upperCase=true}'/>
                <attribute name='Call Site' layout='${callsite}'/>

                <attribute name='Data' encode='false' >
                    <layout type='JsonLayout'
                            includeAllProperties="true"
                            maxRecursionLimit="20" 
                            excludeProperties="Name"
                        >
                        <attribute name='message' layout='${message}' encode='false' />
                        <attribute name='exception' layout='${exception}' />
                    </layout>
                </attribute>
            </layout>

Then use the structured logging call of:

 _logger.Debug("Station Details:{@0}", Station);

  

The NLog output still includes the Name attribute in the produced log. In this example its just the name property however other objects include large file data being brought down from a DB and I would like to exclude those specific properties...

So how do I specifically target the Name Property of the Station Object remembering that the "Name" Property will exists in other objects.

I've tried the following Attributes and they all still include the Name property in the output.

 excludeProperties="_Name"
    excludeProperties="Name"
    excludeProperties="Station_Name"
    excludeProperties="IStation_Name"

What is the correct "Syntax" for ignoring properties when using structured logging in NLog?

1 Answers1

2

The excludeProperties-option controls whether to exclude one or more LogEventInfo.Properties based on property-name. It does not control which object-properties to exclude from the actual property-values.

This will capture Station-value and store it with the property-name secret1:

 _logger.Debug("Station Details:{@secret1}", Station);

This will exclude any property-values with the property-name secret1 or secret2:

 excludeProperties="secret1,secret2"

If you want to control what object-properties to include from a property-value-type, then you can do this instead:

    NLog.LogManager.Setup().SetupSerialization(s =>
       s.RegisterObjectTransformation<Station>(station => new {
          ID = station.ID,
          MACAddress = station.MACAddress,
          ComputerName = station.ComputerName,
       })
    );

See also: https://github.com/NLog/NLog/wiki/How-to-use-structured-logging#transform-captured-properties

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
  • Perfect, that makes more sense now thanks for the example. – Shannon Roos Jan 24 '21 at 22:55
  • It would be good to then create a custom attribute to tag the object property of a Data Model for example so you then don't need to maintain a second set of object property Transformations. Similar to how JSON.net does [JsonIgnore] attribute. – Shannon Roos Jan 24 '21 at 23:13
  • @ShannonRoos You can always consider replacing the default NLog JsonSerializer with Json.Net - https://github.com/NLog/NLog/wiki/How-to-use-structured-logging#i-like-to-use-jsonnet-for-creating-json – Rolf Kristensen Jan 24 '21 at 23:15