Skip to content
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

Support logging on DB level #2557

Closed
Saibamen opened this issue Jul 28, 2020 · 6 comments · Fixed by #2787
Closed

Support logging on DB level #2557

Saibamen opened this issue Jul 28, 2020 · 6 comments · Fixed by #2787

Comments

@Saibamen
Copy link

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);
}
@ErikEJ
Copy link
Contributor

ErikEJ commented Jul 28, 2020

There is a new API for logging now: https://erikej.github.io/efcore/2020/05/18/ef-core-simple-logging.html

@roji
Copy link
Member

roji commented Jul 28, 2020

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.

@roji roji closed this as completed Jul 28, 2020
@Saibamen
Copy link
Author

Saibamen commented Jul 28, 2020

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?

@roji roji reopened this Jul 28, 2020
@ajcvickers
Copy link
Member

@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);
}

@ErikEJ
Copy link
Contributor

ErikEJ commented Aug 3, 2020

Could be a useful addition to the docs.

@Saibamen
Copy link
Author

Saibamen commented Aug 3, 2020

Thanks

@Saibamen Saibamen closed this as completed Aug 3, 2020
@ajcvickers ajcvickers reopened this Aug 3, 2020
@ajcvickers ajcvickers transferred this issue from dotnet/efcore Aug 3, 2020
@ajcvickers ajcvickers self-assigned this Aug 7, 2020
@ajcvickers ajcvickers added this to the Backlog milestone Aug 7, 2020
ajcvickers added a commit that referenced this issue Oct 17, 2020
**No TOC yet since we need to decide what to do with `Fundamentals`. @smitpatel I do not intend to merge this way.**

* Fixes #2615: Add information on controlling suppressing warnings and otherwise configuring EventIds
* Fixes #2557: Support logging on DB level #2557
* Fixes #2770: Add entry in TOC for logging/interceptors
* Fixes #583: Document how to use DiagnosticSource/DiagnosticListener
* Fixes #673: Document EnableSensitiveDataLogging
* Fixes #1913: Interception of database operations: InterceptionResult or InterceptionResult<DbDataReader>?
* Fixes #1543: Document database interceptors with examples
* Fixes #955: Document EnableRichDataErrorHandling
* Fixes #661: EF Core 2.1: document state change events
ajcvickers added a commit that referenced this issue Oct 17, 2020
**No TOC yet since we need to decide what to do with `Fundamentals`. @smitpatel I do not intend to merge this way.**

* Fixes #2615: Add information on controlling suppressing warnings and otherwise configuring EventIds
* Fixes #2557: Support logging on DB level #2557
* Fixes #2770: Add entry in TOC for logging/interceptors
* Fixes #583: Document how to use DiagnosticSource/DiagnosticListener
* Fixes #673: Document EnableSensitiveDataLogging
* Fixes #1913: Interception of database operations: InterceptionResult or InterceptionResult<DbDataReader>?
* Fixes #1543: Document database interceptors with examples
* Fixes #955: Document EnableRichDataErrorHandling
* Fixes #661: EF Core 2.1: document state change events
ajcvickers added a commit that referenced this issue Oct 17, 2020
**No TOC yet since we need to decide what to do with `Fundamentals`. @smitpatel I do not intend to merge this way.**

* Fixes #2615: Add information on controlling suppressing warnings and otherwise configuring EventIds
* Fixes #2557: Support logging on DB level #2557
* Fixes #2770: Add entry in TOC for logging/interceptors
* Fixes #583: Document how to use DiagnosticSource/DiagnosticListener
* Fixes #673: Document EnableSensitiveDataLogging
* Fixes #1913: Interception of database operations: InterceptionResult or InterceptionResult<DbDataReader>?
* Fixes #1543: Document database interceptors with examples
* Fixes #955: Document EnableRichDataErrorHandling
* Fixes #661: EF Core 2.1: document state change events
ajcvickers added a commit that referenced this issue Oct 17, 2020
**No TOC yet since we need to decide what to do with `Fundamentals`. @smitpatel I do not intend to merge this way.**

* Fixes #2615: Add information on controlling suppressing warnings and otherwise configuring EventIds
* Fixes #2557: Support logging on DB level #2557
* Fixes #2770: Add entry in TOC for logging/interceptors
* Fixes #583: Document how to use DiagnosticSource/DiagnosticListener
* Fixes #673: Document EnableSensitiveDataLogging
* Fixes #1913: Interception of database operations: InterceptionResult or InterceptionResult<DbDataReader>?
* Fixes #1543: Document database interceptors with examples
* Fixes #955: Document EnableRichDataErrorHandling
* Fixes #661: EF Core 2.1: document state change events
@ajcvickers ajcvickers modified the milestones: Backlog, 5.0.0 Oct 17, 2020
ajcvickers added a commit that referenced this issue Nov 9, 2020
**No TOC yet since we need to decide what to do with `Fundamentals`. @smitpatel I do not intend to merge this way.**

* Fixes #2615: Add information on controlling suppressing warnings and otherwise configuring EventIds
* Fixes #2557: Support logging on DB level #2557
* Fixes #2770: Add entry in TOC for logging/interceptors
* Fixes #583: Document how to use DiagnosticSource/DiagnosticListener
* Fixes #673: Document EnableSensitiveDataLogging
* Fixes #1913: Interception of database operations: InterceptionResult or InterceptionResult<DbDataReader>?
* Fixes #1543: Document database interceptors with examples
* Fixes #955: Document EnableRichDataErrorHandling
* Fixes #661: EF Core 2.1: document state change events
ajcvickers added a commit that referenced this issue Nov 10, 2020
* Fixes #2615: Add information on controlling suppressing warnings and otherwise configuring EventIds
* Fixes #2557: Support logging on DB level #2557
* Fixes #2770: Add entry in TOC for logging/interceptors
* Fixes #583: Document how to use DiagnosticSource/DiagnosticListener
* Fixes #673: Document EnableSensitiveDataLogging
* Fixes #1913: Interception of database operations: InterceptionResult or InterceptionResult<DbDataReader>?
* Fixes #1543: Document database interceptors with examples
* Fixes #955: Document EnableRichDataErrorHandling
* Fixes #661: EF Core 2.1: document state change events
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants