Logging Ef Core Activity and SQL Slides
Logging Ef Core Activity and SQL Slides
and SQL
Julie Lerman
EF Core Expert and Software Coach
@julielerman | thedatafarm.com
Module
Overview Learn how EF Core extends .NET logging
Adding EF Core logging to the app
Many ways to filter what’s captured and
format how it’s output
Output to various targets
Adding Logging to EF Core’s Workflow
EF Core’s logging is an
extension of
.NET Logging APIs
EF Core Logging Capabilities
EF Core captures:
- SQL
- ChangeTracker activity
- Interaction with database
- Database transactions
EF Core specific configurations:
- EnableDetailedErrors, EnableSensitiveData
- Filter based on message type
(e.g., Database messages)
- Even more detailed filtering (see docs)
We’ll focus on adding
logging to our console app,
then apply these lessons
later to an ASP.NET Core
app.
Simple Logging
Minimal API
No extra NuGet packages
DbContextOptionsBuilder.LogTo Method
LogLevel enums
Debug (default)
Error
Critical
Information
Trace
Warning
None
Additional Filtering Capabilities
Even More Logging Features
Formatting
optionsBuilder
.UseSqlServer(connectionString)
.LogTo(Console.WriteLine)
.EnableSensitiveDataLogging(); t ...using this OptionsBuilder method
Specifying the Target of the Log Output
Configuring Targets via .NET Logging API
Console
File via Streamwriter
Debug window
External logger APIs
LogTo Target Examples
t Delegate to StreamWriter.WriteLine
private StreamWriter _writer Be sure to dispose StreamWriter to save file
= new StreamWriter
(“EFCoreLog.txt", append: true);
optionsBuilder
.LogTo(_writer.WriteLine)
github.com/julielerman/EFCoreEncodingDe
mo
Logs are useful for debugging and learning
EF Core logs provide more than just SQL
Review
You’ll probably want to use Simple Logging
API (LogTo)
EF Core logging extends .NET logging APIs
Explicitly configure sensitive data in
parameters to show in logs
Many ways to customize and filter EF
Core’s logging output
Up Next: