Search code examples
postgresqlentity-framework-corenpgsqlef-core-5.0

Log notices with Npgsql and EF Core


I'm using Npgsql 5 with EF Core 5.

I added "Microsoft.EntityFrameworkCore": "Information" to my appsettings.json.

Suppose I have a sql command that includes this:

RAISE NOTICE 'foo';

How do I configure the provider to log that?


Solution

  • PostgreSQL notices are raised by the Npgsql ADO.NET provider as a regular .NET event. You can register a handler for it as follows:

    conn.Notice += (_, eventArgs) => Console.WriteLine(eventArgs.Notice.MessageText);
    

    You can replace Console.WriteLine with anything that would log it to your logging framework.

    There is currently no integration to automatically route this via EF Core's logging, I've opened https://github.com/npgsql/efcore.pg/issues/2106 to do this for 7.0.