Skip to content

Support logging on DB level #2557

Closed

Description

@Saibamen

I'm migration from EF to EF Core 5.0 (preview 6 now), but there is no Log in DbContext.Database anymore.

using (MyDBContext db = ContextFactory.GetMyContext())
{
    db.Database.Log = logInfo => FileLogger.Log(logInfo);
}

Activity

  1. ErikEJ commented on Jul 28, 2020

    @ErikEJ
    Contributor
  2. roji commented on Jul 28, 2020

    @roji
    Member

    The API linked to above has been introduced for 5.0, which hasn't yet been released. Until that happens, you can still configure logging in EF Core as detailed in this page.

  3. Saibamen commented on Jul 28, 2020

    @Saibamen
    Author

    Ok, but this change logger for all entire DBContext.
    In project which I am upgrading to EF Core there is only one call to DbContext.Database.Log - in only one controller endpoint.
    How to achieve this in EF Core 5.0 preview 6?

  4. reopened this on Jul 28, 2020
  5. ajcvickers commented on Aug 3, 2020

    @ajcvickers
    Contributor

    @Saibamen You could setup a switch on the context to allow logging to be switched on when needed. For example:

    public class SomeDbContext : DbContext
    {
        public bool LoggingEnabled { get; set; }
        
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .LogTo(s =>
                {
                    if (LoggingEnabled)
                    {
                        Console.WriteLine(s);
                    }
                })
                .EnableSensitiveDataLogging()
                .UseSqlServer(Your.SqlServerConnectionString);
    }
  6. ErikEJ commented on Aug 3, 2020

    @ErikEJ
    Contributor

    Could be a useful addition to the docs.

  7. Saibamen commented on Aug 3, 2020

    @Saibamen
    Author

    Thanks

  8. transferred this issue fromdotnet/efcoreon Aug 3, 2020
  9. self-assigned this
    on Aug 7, 2020
  10. added this to the Backlog milestone on Aug 7, 2020
  11. added 4 commits that reference this issue on Oct 17, 2020
    fcd6ed5
    ecf0d01
    4b194ad
    777e0c0
  12. modified the milestones: Backlog, 5.0.0 on Oct 17, 2020
  13. added a commit that references this issue on Nov 9, 2020
    07365ee
  14. added a commit that references this issue on Nov 10, 2020
    18482b0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions