简体   繁体   English

如何忽略 Serilog 中的空属性

[英]how to ignore null properties in Serilog

I want to ignore the null properties in the output window while using serilog.我想在使用 serilog 时忽略输出窗口中的空属性。 the properties which will be null should be ignored and the rest of the properties should be logged.应该忽略为 null 的属性,并且应该记录其余的属性。

Have already looked into AttributedDestructuringPolicy but of no help.已经研究了AttributedDestructuringPolicy但没有帮助。 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请参阅以下链接以了解我的问题: 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 ?知道我怎么能做到这一点吗?

use reflection like this code to get all properties and check it is null or not like this code像这段代码一样使用reflection来获取所有属性并检查它是否为 null 像这段代码一样

 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在这个例子中,删除了一些代码,请自行修复谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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