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 ?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.