Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core / EF.Core 3+ add Console logging to DbContext

I'm wondering how to add console logging to EF.Core without getting an "obsolete" warning.

Given: DbContextOptionsBuilder<BenchmarkDbContext> builder = ...;

I tried:

builder.UseLoggerFactory(new LoggerFactory().AddConsole())

And tried:

var factory = new LoggerFactory();

factory.AddProvider(new ConsoleLoggerProvider((a, b) => true, true));

builder.UseLoggerFactory(factory)
like image 609
Node.JS Avatar asked Dec 04 '22 17:12

Node.JS


1 Answers

I think the initialization of logger is changed in EFCore 3.x. Try using this:

public static readonly ILoggerFactory factory 
    = LoggerFactory.Create(builder => { builder.AddConsole(); });

See details here.

like image 142
vendettamit Avatar answered Jan 09 '23 10:01

vendettamit