Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,61 @@ namespace Serilog.Exceptions.EntityFrameworkCore.Destructurers;
/// <seealso cref="ExceptionDestructurer" />
public class DbUpdateExceptionDestructurer : ExceptionDestructurer
{
/// <inheritdoc />
public override Type[] TargetTypes => new[] { typeof(DbUpdateException), typeof(DbUpdateConcurrencyException) };

/// <inheritdoc />
public override void Destructure(
Exception exception,
IExceptionPropertiesBag propertiesBag,
Func<Exception, IReadOnlyDictionary<string, object?>?> destructureException)
{
base.Destructure(exception, propertiesBag, destructureException);

var dbUpdateException = (DbUpdateException)exception;
var entriesValue = dbUpdateException.Entries?
.Select(
e => new
private readonly int? _entryCountLimit;

/// <summary>
/// Initializes a new instance of the <see cref="DbUpdateExceptionDestructurer"/> class.
/// </summary>
/// <param name="entryCountLimit">Limit of how many entries will be emitted. Null for unlimited.</param>
public DbUpdateExceptionDestructurer(int? entryCountLimit = null)
{
_entryCountLimit = entryCountLimit;
}

/// <inheritdoc />
public override Type[] TargetTypes => new[]
{
typeof(DbUpdateException),
typeof(DbUpdateConcurrencyException)
};

/// <inheritdoc />
public override void Destructure(
Exception exception,
IExceptionPropertiesBag propertiesBag,
Func<Exception, IReadOnlyDictionary<string, object?>?> 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());
}
}
}