Search⌘ K
AI Features

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.

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)
        {
        }
    }

}
Simple logging

Click the “Run” button, then execute the command below in the terminal:

C#
dotnet run

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

}
Logging to a file

Click the “Run” button, then execute the command below in the terminal:

C#
dotnet run

Note: From the SPA terminal, we can view the content of the newly created logs.txt file with the Linux command cat logs.txt, where logs.txt is the name of the log file.

Note the following from the project above:

  • Line 9 of ArtistsContext.cs creates an object of the StreamWriter class.

  • Line 22 of ArtistsContext.cs passes this object to the LogTo method while calling the WriteLine method of the StreamWriter.

  • Lines 30–40 of ArtistsContext.cs dispose of the StreamWriter object.