Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for limiting entries in DbUpdateExceptionDestructurer #673

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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());
}
}
}