6

如果我有以下课程

public class Customer
{
    public string Name;
}

然后在 Serilog 中有以下日志命令

Log.Logger = new LoggerConfiguration()
    .WriteTo.Console()
    .WriteTo.Seq("http://localhost:5341")
    .CreateLogger();

var item = new Customer();
item.Name = "John";
Serilog.Log.Information("Customer {@item}", item);

日志仅在 Seq 中显示为

Customer {}

如果我将 Name 字段更改为它可以工作的属性,但我不想在这个阶段这样做。有什么办法吗?

4

2 回答 2

11

要仅针对一种类型(推荐)执行此操作,您可以使用:

.Destructure.ByTransforming<Customer>(c => new { c.Name })

如果你想包含所有类型的公共字段,或者匹配某种条件的公共字段,你可以插入一个策略来做到这一点:

class IncludePublicFieldsPolicy : IDestructuringPolicy
{
    public bool TryDestructure(
        object value,
        ILogEventPropertyValueFactory propertyValueFactory,
        out LogEventPropertyValue result)
    {
        if (!(value is SomeBaseType))
        {
            result = null;
            return false;
        }

        var fieldsWithValues = value.GetType().GetTypeInfo().DeclaredFields
            .Where(f => f.IsPublic)
            .Select(f => new LogEventProperty(f.Name,
               propertyValueFactory.CreatePropertyValue(f.GetValue(value))));

        result = new StructureValue(fieldsWithValues);
        return true;
    }
}

该示例将其范围缩小到仅查看派生自的对象SomeBaseType

您可以将其插入:

.Destructure.With<IncludePublicFieldsPolicy>()

(我认为这可能需要一些调整,但应该是一个很好的起点。)

于 2017-09-11T22:41:19.427 回答
3

感谢 Nicholas Blumhardt 提供了一个好的起点。我只是有一个小调整。

我的课:

public class Dummy
{
    public string Field = "the field";
    public string Property { get; set; } = "the property";
}

日志调用:

Log.Information("Dummy = {@Dummy}", new Dummy());

IDestructuringPolicy 实现包括字段和属性:

internal class IncludePublicFieldsPolicy : IDestructuringPolicy
{
    public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
    {
        var typeInfo = value.GetType().GetTypeInfo();

        var fieldsWithValues = typeInfo
            .DeclaredFields
            .Where(f => f.IsPublic)
            .Select(f =>
            {
                var val = f.GetValue(value);
                var propval = propertyValueFactory.CreatePropertyValue(val);
                var ret = new LogEventProperty(f.Name, propval);
                return ret;
            })
        ;

        var propertiesWithValues = typeInfo
            .DeclaredProperties
            .Where(f => f.CanRead)
            .Select(f =>
            {
                var val = f.GetValue(value);
                var propval = propertyValueFactory.CreatePropertyValue(val);
                var ret = new LogEventProperty(f.Name, propval);
                return ret;
            })
        ;

        result = new StructureValue(fieldsWithValues.Union(propertiesWithValues));
        return true;
    }
}
于 2020-02-15T20:16:30.057 回答