ASP.NET Core  

How to Configure Serilog in .NET 8 for Logging to File and Console

Introduction

Logging is one of the most important parts of any application. When something goes wrong, logs help you understand what happened, where it happened, and why it happened.

In .NET 8 applications, logging becomes even more important because modern applications are distributed, scalable, and often deployed in cloud environments.

Serilog is a powerful and popular logging library in the .NET ecosystem that makes logging simple, structured, and flexible.

In this guide, you will learn how to configure Serilog in .NET 8 to log messages to both the console and a file in a simple and practical way.

What is Serilog?

Serilog is a structured logging library for .NET applications.

In simple words:

  • It helps you write logs in a clean and structured format

  • It supports multiple output targets (console, file, database, etc.)

  • It makes debugging and monitoring easier

Unlike traditional logging, Serilog allows you to log data in a structured way, which is very useful in real-world applications.

Why Use Serilog in .NET 8?

Using Serilog gives you several advantages:

  • Easy to configure and use

  • Supports structured logging

  • Works well with modern .NET applications

  • Can log to multiple destinations at the same time

  • Helps in debugging and production monitoring

Prerequisites

Before starting, make sure you have:

  • .NET 8 SDK installed

  • A working ASP.NET Core or Console application

  • Basic understanding of Program.cs file

Step 1: Install Required NuGet Packages

You need to install the following packages:

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

These packages allow Serilog to integrate with ASP.NET Core and write logs to console and file.

Step 2: Configure Serilog in Program.cs

Open your Program.cs file and configure Serilog.

using Serilog;

var builder = WebApplication.CreateBuilder(args);

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Information()
    .WriteTo.Console()
    .WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Day)
    .CreateLogger();

builder.Host.UseSerilog();

var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();

Explanation in simple terms:

  • MinimumLevel.Information() → Logs messages from Information level and above

  • WriteTo.Console() → Writes logs to console

  • WriteTo.File() → Writes logs to a file

  • rollingInterval: Day → Creates a new log file every day

Step 3: Logging Messages in Application

Now you can log messages using Serilog.

Log.Information("Application started successfully");
Log.Warning("This is a warning message");
Log.Error("This is an error message");

These logs will appear both in the console and in the log file.

Step 4: Using ILogger in Controllers

In ASP.NET Core, you typically use ILogger.

public class HomeController : Controller
{
    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger)
    {
        _logger = logger;
    }

    public IActionResult Index()
    {
        _logger.LogInformation("Home page accessed");
        return View();
    }
}

Serilog automatically integrates with ILogger, so you don’t need extra setup.

Step 5: Structured Logging Example

One of the best features of Serilog is structured logging.

Log.Information("User {UserName} logged in at {Time}", "John", DateTime.Now);

Instead of plain text, this stores data in a structured format, making it easier to analyze.

Step 6: Configure Logging via appsettings.json (Optional)

You can also configure Serilog using appsettings.json.

"Serilog": {
  "MinimumLevel": "Information",
  "WriteTo": [
    { "Name": "Console" },
    {
      "Name": "File",
      "Args": {
        "path": "logs/log.txt",
        "rollingInterval": "Day"
      }
    }
  ]
}

Then update Program.cs:

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

This approach is useful when you want to change logging settings without modifying code.

Step 7: Log Levels Explained

Serilog supports different log levels:

  • Information → General application flow

  • Warning → Something unexpected but not critical

  • Error → Something failed

  • Debug → Detailed debugging info

Choosing the right log level helps in better monitoring.

Step 8: Best Practices for Serilog

Follow these best practices:

  • Use structured logging instead of plain text

  • Avoid logging sensitive data

  • Use appropriate log levels

  • Enable file rolling to manage log size

  • Store logs in a dedicated folder

Step 9: Real-World Use Case

In a real application:

  • Console logs help during development

  • File logs help in production debugging

For example:

  • If an API fails in production, you can check log files

  • If something breaks locally, you can see logs in console

Advantages of Using Serilog

  • Easy integration with .NET 8

  • Supports multiple log outputs

  • Structured and readable logs

  • Helps in debugging and monitoring

Summary

Serilog is a powerful logging library that makes it easy to log messages in .NET 8 applications. By configuring it to write logs to both console and file, you can monitor your application effectively in both development and production environments. With structured logging, proper log levels, and best practices, you can build a reliable logging system that helps you quickly identify and fix issues in your application.