Skip to content

Templates enable EF SQL command logging in development #35139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

DamianEdwards
Copy link
Member

Fixes #32977

@DamianEdwards DamianEdwards requested review from Pilchie and a team as code owners August 6, 2021 23:37
@ghost ghost added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Aug 6, 2021
@DamianEdwards
Copy link
Member Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@davidfowl
Copy link
Member

davidfowl commented Aug 7, 2021

This feels so bad to do preemptively. We shouldn't do this for templates that don't use EF

Copy link
Member

@davidfowl davidfowl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be in the template

@DamianEdwards
Copy link
Member Author

@davidfowl are you saying you don't think this should be in the templates at all, or that we should remove it from the templates that don't use Entity Framework?

@davidfowl
Copy link
Member

I think adding it to templates that use EF is fine.

@ajcvickers
Copy link
Contributor

Would it be useful to add a template/option to use EF Core without needing to also use Identity? //cc @Rick-Anderson @JeremyLikness

@davidfowl Maybe the real problem here is the setting to Warning for all Microsoft logs, which sets the logging for any Microsoft package without any knowledge of what the packages are or what is appropriate to log from them. Can the template more selectively set logging levels for only the things it actually knows about?

@Rick-Anderson
Copy link
Contributor

I want to make sure customers that create a new web or API project have EF logging in debug mode enabled. See this SO thread, 55K + views, all the ridiculous answers to get SQL logging. Luckily I was able to contact the owner and have him change my answer to the correct answer.

@JeremyLikness
Copy link
Member

Which templates use EF Core? Is it just when users choose authentication and pick identity server? Or are there templates that will scaffold general data access (not Identity) using EF Core? I thought it was the former. Either way I agree there shouldn't be EF Core settings in templates that don't use EF Core.

@DamianEdwards
Copy link
Member Author

@JeremyLikness yes it's only when the identity options are enabled that EF Core is added to the templates today. The scaffolding system will add EF Core to a project but doesn't currently add the logging configuration. I've created dotnet/Scaffolding#1615 to track that.

I'll update the PR to remove EF Core logging settings if EF Core is not actually referenced.

@ajcvickers
Copy link
Contributor

@DamianEdwards Do we have numbers that show scaffolding commonly used to add EF Core? (In other words, will this really address @Rick-Anderson's concerns?)

@DamianEdwards
Copy link
Member Author

New idea from @davidfowl:

Instead of making everything Microsoft.* Warning, how about we configure just Microsoft.AspNetCore.* and Microsoft.Extensions.* as Warning?

@DamianEdwards
Copy link
Member Author

DamianEdwards commented Aug 9, 2021

Just tried this out locally and if we want this change to only apply for development then this is what the logging section of appsettings.Development.json has to contain to get the desired configuration:

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Information",
      "Microsoft.AspNetCore": "Warning",
      "Microsoft.Extensions": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }

This is because these settings are merged with those in appsettings.json which explicitly configures "Microsoft": "Warning" so we have to override it.

Alternatively we update both configuration files with the same settings like so:

"Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning",
      "Microsoft.Extensions": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }

This will potentially result in more logs by default in Production (i.e. anything from Microsoft.* that isn't Microsoft.AspNetCore or Microsoft.Extensions) but I think we're OK with that on a major release.

@DamianEdwards
Copy link
Member Author

FYI here's some example output from an app using auth with the suggested configuration after starting it then navigating to the profile page (already logged in):

dotnet run
Building...
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7151
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5057
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\src\GitHub\dotnet\aspnetcore\src\ProjectTemplates\scripts\webapp
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 6.0.0-rc.1.21406.1 initialized 'ApplicationDbContext' using provider 'Microsoft.EntityFrameworkCore.Sqlite' with options: None
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (11ms) [Parameters=[@__p_0='?' (Size = 36)], CommandType='Text', CommandTimeout='30']
      SELECT "a"."Id", "a"."AccessFailedCount", "a"."ConcurrencyStamp", "a"."Email", "a"."EmailConfirmed", "a"."LockoutEnabled", "a"."LockoutEnd", "a"."NormalizedEmail", "a"."NormalizedUserName", "a"."PasswordHash", "a"."PhoneNumber", "a"."PhoneNumberConfirmed", "a"."SecurityStamp", "a"."TwoFactorEnabled", "a"."UserName"
      FROM "AspNetUsers" AS "a"
      WHERE "a"."Id" = @__p_0
      LIMIT 1

@davidfowl
Copy link
Member

Remove "Microsoft.Extensions": "Warning", and see what happens?

@DamianEdwards
Copy link
Member Author

DamianEdwards commented Aug 9, 2021

@davidfowl sorry I already had, so what you're seeing above is the output from both settings files having:

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }

@DamianEdwards
Copy link
Member Author

I guess we can remove the specific config for Microsoft.Hosting.Lifetime too then...

@DamianEdwards
Copy link
Member Author

Confirmed that we can update both settings files with following and get the same output:

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  }

@davidfowl
Copy link
Member

cc @Tratcher

Also made gRPC match the rest.
@DamianEdwards DamianEdwards requested a review from davidfowl August 9, 2021 23:27
@DamianEdwards DamianEdwards merged commit 59cca98 into main Aug 10, 2021
@DamianEdwards DamianEdwards deleted the damianedwards/enable-ef-sql-logging-templates-32977 branch August 10, 2021 00:44
@ghost ghost added this to the 6.0-rc1 milestone Aug 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ASP.NET template disables EF Core SQL logging by default
5 participants