1

我正在编写最简单的丰富器:找到所有XProperty具有枚举值类型的属性并添加另一个名为的属性XPropertyString,它是枚举值的字符串表示形式。

我从这样的事情开始:

    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);
                }
            }
        }
    }

似乎属性值是“包装的”Serilog 的类型Serilog.Events.ScalarValueSerilog.Events.SequenceValue

有什么方法可以获取上面示例中的基础属性值类型?

4

1 回答 1

1
        foreach(var p in logEvent.Properties)
        {
            if (p.Value is ScalarValue sv)
            {
                Type t = sv.Value?.GetType();
于 2020-08-05T23:05:14.840 回答