0

What is the best method to log EF core 5.0 queries to the console of Blazor server .NET 5?

I'm wondering why there aren't any threads about this topic.

  • The DB is a server concern, not a client one. You shouldn't be exposing that kind of information to users. Look in the Output window in Visual Studio, or in the console window of your server and you'll see the SQL there. – Peter Morris Aug 21 '20 at 21:29
  • Thanks, I would like to inspect the queries in Visual Studio, the problem is that both Output and Immediate Window are both empty. – user3127438 Aug 21 '20 at 22:39
  • 1
    @PeterMorris "Blazor server" would be running EF on the server, not the client. https://learn.microsoft.com/en-us/dotnet/architecture/blazor-for-web-forms-developers/hosting-models – David Browne - Microsoft Aug 21 '20 at 23:07
  • David, I know how it works, thanks, I explain it on Blazor-university.com, but the Browser has a console and that's where I was saying it shouldn't appear - in case that's what the questioner wanted. – Peter Morris Aug 22 '20 at 08:47
  • Try this https://www.entityframeworktutorial.net/efcore/logging-in-entityframework-core.aspx#:~:text=UseLoggerFactory()%20method%20in%20the,such%20as%20filter%20parameter%20values. – Peter Morris Aug 22 '20 at 08:49
  • Thanks Peter, I've tried that already but I can't seem to get it working. `public static readonly ILoggerFactory loggerFactory = new LoggerFactory(new[] {new ConsoleLoggerProvider((_, __) => true, true)});` First I get: error CS0246: The type or namespace name 'ILoggerFactory' could not be found (are you missing a using directive or an assembly reference?) I added: using Microsoft.Extensions.Logging; Then I get the error: 'ConsoleLoggerProvider' does not contain a constructor that takes 2 arguments I'm using .NET 5 preview 7 and EF Core 5.0 – user3127438 Aug 23 '20 at 08:23
  • `I'm wondering why there aren't any threads about this topic.` there are, a *lot*. In fact this is a duplicate of *many* other questions. Google for `EF Core Logging`. You have to configure logging first, add the Console as a sink/logging provider and enable EF Core logging. It doesn't matter whether your application is Razor Pages, Web API or Blazor Server (or even a console tool), logging and EF Core work the same – Panagiotis Kanavos Aug 23 '20 at 16:23
  • The EF Core docs describe [how to configure logging](https://learn.microsoft.com/en-us/ef/core/miscellaneous/logging?tabs=v3). In ASP.NET Core applications, logging is enabled by default if you use `AddDbContext` or `AddDbContextPool`. You don't need to explicitly use specify either the logging factor or the Console logging provider. For one thing, logging is already configured when the WebHost was created. There's no need to create a singleton LoggingFactory – Panagiotis Kanavos Aug 23 '20 at 16:27
  • Does this answer your question? [How to enable logging in EF Core 3?](https://stackoverflow.com/questions/56646186/how-to-enable-logging-in-ef-core-3) – Panagiotis Kanavos Aug 23 '20 at 16:31
  • BTW I can't believe I had already answered this question before. I had completely forgotten about it .... – Panagiotis Kanavos Aug 23 '20 at 16:31
  • Thanks you Panagiotis taking your time to answer this. I'm aware that there are many threads available for EF core logging. but none of them seems to work with Blazor .NET 5 preview 7 and EF Core 5.0. Also the variant that you mention `public static readonly ILoggerFactory MyLoggerFactory = LoggerFactory.Create(builder => { builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();});` I've tried already I didn't got an error, but there is also no logging information in the Output/Immediate window in Visual Studio – user3127438 Aug 23 '20 at 19:00

0 Answers0