diff --git a/Source/Serilog.Exceptions.EntityFrameworkCore/Destructurers/DbUpdateExceptionDestructurer.cs b/Source/Serilog.Exceptions.EntityFrameworkCore/Destructurers/DbUpdateExceptionDestructurer.cs index 3e3391ea..b6cfeb56 100644 --- a/Source/Serilog.Exceptions.EntityFrameworkCore/Destructurers/DbUpdateExceptionDestructurer.cs +++ b/Source/Serilog.Exceptions.EntityFrameworkCore/Destructurers/DbUpdateExceptionDestructurer.cs @@ -13,34 +13,61 @@ namespace Serilog.Exceptions.EntityFrameworkCore.Destructurers; /// public class DbUpdateExceptionDestructurer : ExceptionDestructurer { - /// - public override Type[] TargetTypes => new[] { typeof(DbUpdateException), typeof(DbUpdateConcurrencyException) }; - - /// - public override void Destructure( - Exception exception, - IExceptionPropertiesBag propertiesBag, - Func?> destructureException) - { - base.Destructure(exception, propertiesBag, destructureException); - - var dbUpdateException = (DbUpdateException)exception; - var entriesValue = dbUpdateException.Entries? - .Select( - e => new + private readonly int? _entryCountLimit; + + /// + /// Initializes a new instance of the class. + /// + /// Limit of how many entries will be emitted. Null for unlimited. + public DbUpdateExceptionDestructurer(int? entryCountLimit = null) + { + _entryCountLimit = entryCountLimit; + } + + /// + public override Type[] TargetTypes => new[] + { + typeof(DbUpdateException), + typeof(DbUpdateConcurrencyException) + }; + + /// + public override void Destructure( + Exception exception, + IExceptionPropertiesBag propertiesBag, + Func?> destructureException + ) + { + base.Destructure(exception, propertiesBag, destructureException); + + var dbUpdateException = (DbUpdateException)exception; + + if (dbUpdateException.Entries != null) + { + propertiesBag.AddProperty("EntryCount", dbUpdateException.Entries.Count); + + var entriesQuery = dbUpdateException.Entries + .Select( + e => new + { + EntryProperties = e.Properties.Select( + p => new + { + PropertyName = p.Metadata.Name, + p.OriginalValue, + p.CurrentValue, + p.IsTemporary, + p.IsModified, + }), + e.State, + }); + + if (_entryCountLimit != null) { - EntryProperties = e.Properties.Select( - p => new - { - PropertyName = p.Metadata.Name, - p.OriginalValue, - p.CurrentValue, - p.IsTemporary, - p.IsModified, - }), - e.State, - }) - .ToList(); - propertiesBag.AddProperty(nameof(DbUpdateException.Entries), entriesValue); - } + entriesQuery = entriesQuery.Take(_entryCountLimit.Value); + } + + propertiesBag.AddProperty(nameof(DbUpdateException.Entries), entriesQuery.ToList()); + } + } }