0

I want to ignore the null properties in the output window while using serilog. the properties which will be null should be ignored and the rest of the properties should be logged.

Have already looked into AttributedDestructuringPolicy but of no help. Any help or lead would be appreciated.

please see the below link to get an idea about my question: https://github.com/serilog/serilog/issues/1286

I have created a custom method to arrange my log as below :-

 public static Log PrepareLogDetails(
        ActivityState activityState,
        string compressedData,
        string exceptionMessage,
        string exceptionStacktrace,
        string correlationId,
        Initiator initiator,
        string initiatorId,
        Dictionary<string, string> metadata)
    {
        var step = new Step()
        {
            activityState = activityState,
            CompressedData = compressedData.GZipStringCompress()
        };

        if (!string.IsNullOrEmpty(exceptionMessage))
        {
            step.Exception = new Adapter_Exception()
            {
                Message = exceptionMessage,
                StackTrace = exceptionStacktrace
            };
        }

     

        var log = new Log()
        {
            CorrelationId = correlationId,
            Initiator = initiator,
            InitiatorApp = "VPAdjustment",
            InitiatorId = initiatorId,
            Step =step,
            Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
            Metadata= metadata
        };

       
        return log;
    }

Now when I am returning the log object, it contains the null properties as well. I want to ignore those null properties while returning the log object. Any idea how can I achieve this ?

  • From that very issue you linked: _"Copying the code for RenderedCompactJsonFormatter into your app as a new class, and modifying its internal behavior, is the way to go, here."_ – Fildor Aug 26 '20 at 12:53
  • ^^ refers to this: https://github.com/serilog/serilog-formatting-compact/blob/dev/src/Serilog.Formatting.Compact/Formatting/Compact/RenderedCompactJsonFormatter.cs#L26 – Fildor Aug 26 '20 at 12:59
  • @Fildor : I saw this link but didnt find a way to implement this. I have updated my query with example. Please have a look – siddharth chakraborty Aug 27 '20 at 07:28

1 Answers1

0

use reflection like this code to get all properties and check it is null or not like this code

 var _obj = Object;

 //in this line you get all property of class

 var classProperties = _obj.GetType()
                                 .GetProperties(
                                         BindingFlags.Public
                                         | BindingFlags.Instance);

 foreach (var propertyInfo in classProperties)
            {
                if (propertyInfo.PropertyType.IsSealed)
                {
                    var prop = _obj.GetType().GetProperty(propertyInfo.Name).GetValue(_obj, null);
                    if (prop != null && prop.ToString() != string.Empty && prop.ToString() != "0")
                    {
                      
                        var att = propertyInfo.CustomAttributes.FirstOrDefault();
                        if (att != null)
                        {
                            //if attribute not null you can get description 
                            propName.Content = att.ConstructorArguments.FirstOrDefault().Value.ToString();
                        }
                        else
                        {
                            //property name 
                            propName.Content = propertyInfo.Name;
                        }

                        var propValue = prop.ToString();
                    }
                }
            }

in this example some codes remove please fix them by own Thanks

Akbar Asghari
  • 613
  • 8
  • 19