October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to Write Serilog and EF Core Logs to the Same File in ASP.NET Core

Route EF Core’s Microsoft logging events through Serilog to write application and database logs to one file, and troubleshoot missing entries.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Register Serilog as ASP.NET Core’s Microsoft.Extensions.Logging provider and configure a file sink on that same Serilog logger. EF Core’s ordinary logging events then flow through the shared pipeline into the file. By contrast, DbContextOptionsBuilder.LogTo(...) is a separate simple-logging callback; it will not reach that file automatically.

Set up one Serilog pipeline for the application and EF Core

ASP.NET Core applications use the Microsoft.Extensions.Logging abstraction for framework and injected ILogger<T> messages. The Serilog ASP.NET Core integration connects that pipeline to Serilog, allowing application and framework events to use the same levels and destinations. EF Core’s normal logging events use this Microsoft logging pipeline too. See the ASP.NET Core logging documentation and the Serilog.AspNetCore project documentation.

As an Amazon Associate I earn from qualifying purchases.

For a modern minimal-hosting application, the arrangement looks like this:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
using Serilog;

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseSerilog((context, services, configuration) => configuration
    .ReadFrom.Configuration(context.Configuration)
    .ReadFrom.Services(services)
    .WriteTo.File("logs/app-.log", rollingInterval: RollingInterval.Day));

var app = builder.Build();
app.Run();

The key is that the file sink belongs to the Serilog logger registered with the host. Configuring only Serilog’s static Log API does not, on its own, route injected ILogger<T> events through that logger.

Serilog.AspNetCore’s current README also demonstrates registering Serilog with builder.Services.AddSerilog() and closing and flushing the logger in a finally block. The precise overloads and registration pattern depend on the installed Serilog.AspNetCore release. Select a package major version that matches the application’s target framework and verify the API signature against that release’s README rather than copying a version-specific example blindly.

Install the integration and file sink

Add the ASP.NET Core integration and file sink packages:

dotnet add package Serilog.AspNetCore
dotnet add package Serilog.Sinks.File

Serilog’s getting-started documentation shows Serilog.Sinks.File and the WriteTo.File(..., rollingInterval: RollingInterval.Day) pattern. ASP.NET Core does not include a built-in general-purpose file logging provider, so a Serilog sink supplies the file destination.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Make sure EF Core is using the shared provider

EF Core logs written through Microsoft.Extensions.Logging will use Serilog once Serilog is registered as the application’s provider. You generally do not need to add a separate logging call to the DbContext just to put those events in the same file.

Do not add optionsBuilder.LogTo(Console.WriteLine) expecting it to join the Serilog pipeline. EF Core’s LogTo API invokes the callback you provide, so Console.WriteLine sends those messages to the console instead. If you intentionally use LogTo, its callback must explicitly forward events to the desired logging pipeline; otherwise it remains an independent output path. See Microsoft’s EF Core simple logging documentation.

Choose categories and levels so database events are retained

Shared output does not guarantee that every EF Core event will appear. Category and minimum-level filters can exclude events before they reach the file sink. EF Core uses hierarchical categories, including:

  • Microsoft.EntityFrameworkCore.Database.Command for database commands.
  • Microsoft.EntityFrameworkCore.Database.Transaction for transactions.
  • Microsoft.EntityFrameworkCore.Query for queries.
  • Microsoft.EntityFrameworkCore for broader EF Core logging.

Use Serilog category overrides to adjust the signal—for example, retaining database command information without lowering the application-wide minimum level. Check the effective configuration at runtime, including any Microsoft logging filters: a filter upstream of Serilog can prevent an event from reaching the sink. EF Core’s simple-logging documentation describes its own minimum-level, event-ID, category, and predicate filters; those controls apply to the separate LogTo route.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Diagnose missing EF Core entries

  1. Verify provider registration. Confirm the host’s Microsoft logging pipeline is connected to Serilog, rather than configuring only the static Serilog logger.
  2. Verify the sink and path. Confirm the File sink is attached to that registered logger and that the application identity can write to the configured directory.
  3. Check the EF Core output path. Look for a LogTo callback writing somewhere else, such as the console. It does not automatically use the Serilog sink.
  4. Check category and level filters. Ensure the effective settings permit the relevant EF Core categories, especially Microsoft.EntityFrameworkCore.Database.Command, at the events’ levels.
  5. Close the logger during orderly shutdown. Follow the integration’s shutdown pattern to flush and dispose of the logger; the Serilog.AspNetCore README demonstrates closing and flushing in finally.
  6. Check version alignment. Confirm the Serilog.AspNetCore major version and registration API are appropriate for the application’s target framework.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When to use EF Core LogTo instead

LogTo can be useful for focused diagnostics when a callback-based output is what you want. EF Core’s simple logging defaults to Debug and higher and supports filtering by level, event ID, category, or a custom predicate. Its formatting options include multiline output by default, UTC timestamps, and single-line output. These controls belong to the direct-output callback and do not make it part of Serilog’s structured pipeline.

Best Value
Sale
Programming ASP.NET Core (Developer Reference)
  • Applying all key ASP.NET Core components, including MVC for HTML generation, .NET Core, EF Core, ASP.NET Identity, dependency injection, and more
  • Integrating ASP.NET Core with leading client-side frameworks, including Bootstrap
  • ASP.NET Core code for implementing business logic and data transformations
  • Handling configuration, routing, controllers, views, and common tasks (including posting forms and presenting data)
  • Performing complementary tasks: error handling, logging, application design, authentication, localization, and more

For a shared application log file, the simpler fit is Microsoft.Extensions.Logging → Serilog → File sink. Use LogTo as a separate diagnostic route when that separation is intentional, or explicitly forward its callback if it must share a destination.

Quick Recap

Bestseller No. 2
SaleBestseller No. 5
Programming ASP.NET Core (Developer Reference)
Programming ASP.NET Core (Developer Reference)
Integrating ASP.NET Core with leading client-side frameworks, including Bootstrap; ASP.NET Core code for implementing business logic and data transformations
$24.99

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.