Giorgi / EntityFramework.Exceptions

Strongly typed exceptions for Entity Framework Core. Supports SQLServer, PostgreSQL, SQLite, Oracle and MySql.
https://giorgi.dev/entity-framework/introducing-entityframework-exceptions/
Other
1.44k stars 68 forks source link

Get Exception returns all DB context entries, which cause huge log file. and vulnerable to add modified, added data #41

Closed mKamiAsim closed 2 years ago

mKamiAsim commented 2 years ago

Hi,

I received huge log file when DBUpdateException raised. Exception details added Entries of entire DB context along with Models and its attributes. As I am logging complete exception, single log produced 6Mb file.

I found following code in EntityFramework.Exceptions.Common might causing it

private Exception GetException(DbUpdateException ex)
{
    T val = ex.GetBaseException() as T;
 if (val != null)
 {
     DatabaseError? databaseError = GetDatabaseError(val);
    if (databaseError.HasValue)
    {
        DatabaseError valueOrDefault = databaseError.GetValueOrDefault();
        List entries = ex.Entries.Select((EntityEntry entry) => base.GetOrCreateEntry(entry.Entity, entry.Metadata)).ToList();
        return ExceptionFactory.Create(valueOrDefault, ex, entries);
    }
 }
        return null;
 }

Include provider and version information EntityFrameworkCore.Exceptions.SqlServer 3.1.4 EF Core version: 5.0.13 Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Target framework: (e.g. .NET 5.0) Operating system: IDE: (e.g. Visual Studio 2022 17.0.6)

Giorgi commented 2 years ago

Are you using Serilog.Exceptions? If so this could be caused by https://github.com/dotnet/efcore/issues/15214

Even if you aren't you would encounter the same issue if you log the DbUpdateException instance instead of the exception provided by this library. The code you mentioned uses the Entries present on the exception object and doesn't query any entities on it's own.