Get original property value type from ILogEventPropertyFactory

1.4k Views Asked by At

I'm writing the simplest of enrichers: find all properties XProperty that have enum value types and add another property named XPropertyString which is the string representation of the Enum value.

I started with something like this:

    public class EnumStringEnricher : ILogEventEnricher
    {
        public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
        {
            foreach(var p in logEvent.Properties)
            {
                Type t = p.Value?.GetType();
                if (t != null && t.IsEnum)
                {
                    //create property <enum_property>String with Enum description as value
                    LogEventProperty enumString = propertyFactory.CreateProperty($"{p.Key}String", Enum.GetName(t, p.Value));
                    logEvent.AddPropertyIfAbsent(enumString);
                }
            }
        }
    }

It seems that property values are "wrapped-in" Serilog's types Serilog.Events.ScalarValue, Serilog.Events.SequenceValue etc.

Is there any way to get the underlying property value type in the above example?

1

There are 1 best solutions below

0
On BEST ANSWER
        foreach(var p in logEvent.Properties)
        {
            if (p.Value is ScalarValue sv)
            {
                Type t = sv.Value?.GetType();