This repository contains custom INpgsqlLoggingProvider implementation that utilizes Microsoft.Extensions.Logging.ILogger underneath. Npgsql log messages are written to all log providers added, obeying standart Microsoft.Extensions.Logging configuration rules.
To hook up custom INpgsqlLoggingProvider log provider, inject NpgsqlLoggingProviderWithILogger class while building host.
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.ConfigureServices((hostingContext, services) =>
{
services.AddSingleton<INpgsqlLoggingProvider, NpgsqlLoggingProviderWithILogger>();
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.ClearProviders();
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
});
Then enable Npgsql logging on application level, set Npgsql static parameters at the beginning of application, before invoking any other Npgsql method.
public static void Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
//Configure PostgreSQL static parameters
NpgsqlLogManager.Provider = host.Services.GetRequiredService<INpgsqlLoggingProvider>();
//Enable parameter value logging. These may contain sensitive information.
NpgsqlLogManager.IsParameterLoggingEnabled = true;
host.Run();
}
The configuration is setup in the Logging section of the configuraton json file.
Use Npgsql
category under Logging
block to configure log level for all Npgsql logs.
For information on LogLevel configuration, check Logging in .NET Core and ASP.NET Core documentation.
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information",
"Npgsql": "Debug"
}
}