Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

c# - NLog Structured Logging Ignore Property By Name

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?

question from:https://stackoverflow.com/questions/65839859/nlog-structured-logging-ignore-property-by-name

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...