Logging with Entity Framework Core
Explore how to implement simple logging in Entity Framework Core to track and diagnose SQL queries. This lesson guides you through configuring log outputs to the console or file, helping you monitor query performance and troubleshoot issues efficiently.
We'll cover the following...
Simple logging
In EF Core, logging enables us to obtain information about the SQL queries we execute. For instance, logging can diagnose a slow query or a query failing to return the desired results.
Simple logging is a form of logging in EF Core that requires minimal configuration and no additional NuGet packages. It provides an easy means to obtain logs while developing applications. We’ll demonstrate using the C# project below:
using Microsoft.EntityFrameworkCore;
namespace Logging
{
public class ArtistsContext : DbContext
{
public ArtistsContext() { }
public ArtistsContext(DbContextOptions<ArtistsContext> options) : base(options) { }
public virtual DbSet<Employee> Employees { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder
.UseSqlite("data source=output/Artists.db")
.LogTo(Console.WriteLine);
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
}
}Click the “Run” button, then execute the command below in the terminal:
Configuration
We can configure simple logging by calling the LogTo method when configuring the DbContext. Line 21 of ArtistsContext.cs in the project above highlights this.
Directing the logs
We can direct the log output to a console window, a file or
debug window. The LogTo method takes an Action<T> delegate that accepts a string. EF Core calls this delegate with a string for each log message generated.
Logging to the console
Line 21 of ArtistsContext.cs in the project uses Console.WriteLine as the Action<T> delegate to log to the console.
Logging to a file
We can direct logs to a file. The project below demonstrates this:
using Microsoft.EntityFrameworkCore;
namespace Logging
{
public class ArtistsContext : DbContext
{
private readonly StreamWriter _logWriter = new StreamWriter("logs.txt", true);
public ArtistsContext() { }
public ArtistsContext(DbContextOptions<ArtistsContext> options) : base(options) { }
public virtual DbSet<Employee> Employees { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder
.UseSqlite("data source=output/Artists.db")
.LogTo(_logWriter.WriteLine);
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
public override void Dispose()
{
base.Dispose();
_logWriter.Dispose();
}
public override async ValueTask DisposeAsync()
{
await base.DisposeAsync();
await _logWriter.DisposeAsync();
}
}
}Click the “Run” button, then execute the command below in the terminal:
Note: From the SPA terminal, we can view the content of the newly created
logs.txtfile with the Linux commandcat logs.txt, wherelogs.txtis the name of the log file.
Note the following from the project above:
-
Line 9 of
ArtistsContext.cscreates an object of theStreamWriterclass. -
Line 22 of
ArtistsContext.cspasses this object to theLogTomethod while calling theWriteLinemethod of theStreamWriter. -
Lines 30–40 of
ArtistsContext.csdispose of theStreamWriterobject.