0% found this document useful (0 votes)
25 views25 pages

Logging Ef Core Activity and SQL Slides

Uploaded by

ahmed11611612
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views25 pages

Logging Ef Core Activity and SQL Slides

Uploaded by

ahmed11611612
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Logging EF Core Activity

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

protected override void OnConfiguring


(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(someconnectionstring)
.LogTo(target);
}
Filtering Log Output with EF Core
Message Categories and LogLevel
EF Core
Message
Categories

Source: EF Core Docs


.NET Logging API Configurations

Output targets Log Levels


e.g., console, file e.g., critical, debug
Configuring LogLevel via .NET Logging API

LogLevel enums
Debug (default)

Error

Critical

Information

Trace

Warning

None
Additional Filtering Capabilities
Even More Logging Features

Formatting

Detailed query error information

Filter on event types

What information goes in a log message

Show sensitive information e.g., parameters


Protecting Your Command Parameters
Command Parameters in Your Logs

Protected by default Expose them with explicit code


Enabling Sensitive
Data to Show in Your
Logs
[__name_0='?' (Size = 4000)] t Parameters hidden by default

[__name_0=‘Sampson' (Size = 4000)] t Parameters exposed ...

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

optionsBuilder t Delegate to Console.WriteLine


.LogTo(Console.WriteLine)

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)

optionsBuilder t Lambda expression for Debug.WriteLine


.LogTo(log=>Debug.WriteLine(log));
Encoding and Logging to the Console
Some encodings, e.g. code page 1256 for Arabic,
won’t display in the VS console. If SQL contains
one of these encodings, here’s a tweak to know.
Console Won’t Output Some Encodings

Default encoding won’t display Override encoding displays the


special characters special characters

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:

Interacting with Related Data


Resources

Entity Framework Core on GitHub: github.com/dotnet/efcore

EF Core Simple Logging Documentation: bit.ly/LoggingDocs

Logging SQL and Change-Tracking Events in EF Core,


MSDN Mag, Oct 2019: msdn.microsoft.com/magazine/mt830355

Repository with Console Encoding help:


github.com/julielerman/EFCoreEncodingDemo

You might also like