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

[Question] how to suppress log messages from EF-Core #48

Closed AplixaMartinMetzler closed 2 years ago

AplixaMartinMetzler commented 2 years ago

Hi Giorgi,

first of all, thanks for your work, i really like this lib.

Its just a side question which i guess is more concern of ef-core, anyway:

Im working with postgres and i have this usecase:


    try
        {
            dbContext.SaveChanges();
        }

        catch (UniqueConstraintException)
        {
            //.... resolve task
            dbContext.SaveChanges();
        }

At the moment of the catch clause EF-Core already has written already a log message:

fail: Microsoft.EntityFrameworkCore.Update[10000]
      An exception occurred in the database while saving changes for context type

Is there any possbility to supress this kind of messages?

Giorgi commented 2 years ago

I think you are out of luck if you want to suppress logging only for unique constraint exceptions but you can disable logging for SaveChanges globally like this:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder
        .ConfigureWarnings(b => b.Ignore(CoreEventId.SaveChangesFailed))
        .LogTo(Console.WriteLine);

Suppress logging an event

AplixaMartinMetzler commented 2 years ago

Ok, thanks for fast response. I will try that.

Matthew-Bright commented 2 years ago

Hi @Giorgi

I am also seeing the "Failed executing DbCommand ..." being logged by entity framework. Would you happen to know what the EventId is for that message so that I can suppress it?

Thanks

-- Matthew

Giorgi commented 2 years ago

@Matthew-Bright CoreEventId.SaveChangesFailed is the eventId not to log any error when SaveChanges is called