Combined Serilog and EF Core Logging to the Same File in ASP .NET Core
DEV Community

Combined Serilog and EF Core Logging to the Same File in ASP .NET Core

Introduction

Learn how to use a single daily file to log regular log messages and EF Core commands with the Serilog packages.

Required NuGet packages

  • Serilog.AspNetCore
  • Serilog.Extensions.Logging.File
  • Serilog.Sinks.Console
  • Serilog.Sinks.File
  • Microsoft.EntityFrameworkCore.SqlServer (for demo code)

Serilog configuration

Add the following Serilog settings to the appsettings.json file, and change the path to where you want to create and write log information.

"Serilog": {
  "Using": [
    "Serilog.Sinks.File"
  ],
  "MinimumLevel": {
    "Default": "Information",
    "Override": {
      "Microsoft": "Warning",
      "Microsoft.EntityFrameworkCore.Database.Command": "Information"
    }
  },
  "WriteTo": [
    {
      "Name": "File",
      "Args": {
        "path": "C:\\Logs\\ef-.log",
        "rollingInterval": "Day",
        "retainedFileCountLimit": 7,
        "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}"
      }
    }
  ]
}

๐Ÿ’ก See also: configuration basics

Adding Serilog to Program.cs

Add the following configuration code to Program.cs (see provided code).

builder
    .Host
    .UseSerilog((context, services, configuration) => configuration
        .ReadFrom.Configuration(context.Configuration)
        .ReadFrom.Services(services)
        .Enrich.FromLogContext());

๐Ÿ’ก If logging fails for any reason, see Debugging and Diagnostics.

Writing code and logging

Once the above and EF Core have been configured in Program.cs, start writing code and logging as shown below.

using Microsoft.EntityFrameworkCore;
using Serilog;
namespace EF_Core3.Pages;

public class IndexModel(Context context) : PageModel
{
    public void OnGet()
    {
        var contactsList = context.Contacts.ToList();
        var customersList = context.Customers
            .Include(c => c.CountryIdentifierNavigation)
            .ToList();

        Log.Information(
            "Retrieved {ContactsCount} " +
            "contacts and {CustomersCount} " +
            "customers from the database.",
            contactsList.Count,
            customersList.Count);
    }
}

Sample log output

The log file for the above request contains entries such as:

2026-09-23 10:26:35.775 -07:00 [WRN] Microsoft.EntityFrameworkCore.Model.Validation
Sensitive data logging is enabled. Log entries and exception messages may include sensitive application data; this mode should only be enabled during development.
2026-09-23 10:26:37.089 -07:00 [INF] Microsoft.EntityFrameworkCore.Database.Command
Executed DbCommand (53ms) [Parameters=[], CommandType='"Text"', CommandTimeout='30']
SELECT [c].[ContactId], [c].[ContactTypeIdentifier], [c].[FirstName], [c].[FullName], [c].[LastName]
FROM [Contacts] AS [c]
2026-09-23 10:26:37.450 -07:00 [INF] Microsoft.EntityFrameworkCore.Database.Command
Executed DbCommand (10ms) [Parameters=[], CommandType='"Text"', CommandTimeout='30']
SELECT [c].[CustomerIdentifier], [c].[City], [c].[CompanyName], [c].[ContactId], [c].[ContactTypeIdentifier], [c].[CountryIdentifier], [c].[Fax], [c].[ModifiedDate], [c].[Phone], [c].[PostalCode], [c].[Region], [c].[Street], [c0].[CountryIdentifier], [c0].[Name]
FROM [Customers] AS [c]
LEFT JOIN [Countries] AS [c0] ON [c].[CountryIdentifier] = [c0].[CountryIdentifier]
2026-09-23 10:26:37.545 -07:00 [INF] Retrieved 91 contacts and 91 customers from the database.

Provided sample code

  • Create the NorthWind2024 database under localdb (best done in SSMS).
  • Under the Scripts folder, run populate.sql under localdb\NorthWind2024 database.

In Program.cs, the COMBINED_LOGS setting (defined under project properties) must be checked. Uncheck it to write two separate log files-one for regular logging and one for EF Core.

See also: Serilog logging and EF Core logging.

Summary

Following the instructions and using the provided code sample will create a single daily combined log file that captures both regular application logs and EF Core command logs.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.