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 configurations
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
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.
Start writing code
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);
}
}
Log file for above
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 Scripts folder run populate.sql under localdb\NorthWind2024 database
In Program.cs COMBINED_LOGS which is defined under project properties. Uncheck to write two 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 code sample will create a single daily combined log file.

Top comments (0)