我认为你最好的选择是实现你自己的ExceptionDecostructor
,像这样:
public class FallbackExceptionDestructurer : ExceptionDestructurer
{
public override Type[] TargetTypes => new[]
{
typeof(Exception),
};
public override void Destructure(
Exception exception,
IExceptionPropertiesBag propertiesBag,
Func<Exception, IReadOnlyDictionary<string, object>> destructureException)
{
base.Destructure(exception, propertiesBag, destructureException);
}
}
这样,您将保留所有可用的解构器,并且基于反射的反射将被忽略,因为FallbackExceptionDestructurer
将覆盖由于此代码导致的所有未知异常:
public override Type[] TargetTypes => new[]
{
typeof(Exception),
};
这就是您注册解构器的方式
.Enrich.WithExceptionDetails(new DestructuringOptionsBuilder()
.WithDefaultDestructurers()
.WithDestructurers(new[] { new FallbackExceptionDestructurer () }))
如果您检查ExceptionDestructurer.Decostruct(...)的源代码,它不使用任何反射 - 只有来自 Exception 类型的众所周知的属性。