---
title: "Configure Serilog with WebApplicationBuilder in .Net Core 6"  
description: "Configure Serilog with WebApplicationBuilder in .Net Core 6"  
author: "Utpal Vishwas"  
published: 2023-11-07  
updated: 2023-11-08  
canonical: https://www.mindstick.com/forum/160465/configure-serilog-with-webapplicationbuilder-in-dot-net-core-6  
category: ".net core"  
tags: ["asp.net core", ".net core", ".net core 6"]  
reading_time: 2 minutes  

---

# Configure Serilog with WebApplicationBuilder in .Net Core 6

[Configure](https://www.mindstick.com/blog/242/how-to-configuring-sql-server-memory-settings) Serilog with WebApplicationBuilder in .Net Core 6

## Replies

### Reply by Aryan Kumar

To configure Serilog with a **WebApplicationBuilder** in ASP.NET Core 6, you can follow these steps. Serilog is a powerful logging library that you can integrate with your ASP.NET Core application for structured and customizable logging.

## Install Serilog Packages:

First, make sure you have the necessary Serilog packages installed. You can do this using NuGet:

```plaintext
using Microsoft.AspNetCore.Hosting;
using Serilog;
using Serilog.Events;

public class Program
{
    public static void Main(string[] args)
    {
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Information() // Minimum log level
            .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) // Override the level for Microsoft logs
            .Enrich.FromLogContext() // Include properties from HttpContext, if available
            .WriteTo.Console() // Log to the console
            .WriteTo.File("log.txt", rollingInterval: RollingInterval.Day) // Log to a file with daily rolling
            .CreateLogger();

        var host = WebApplication.CreateBuilder(args)
            .UseSerilog() // Use Serilog for ASP.NET Core logging
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}
```

These packages include Serilog, Serilog's integration with ASP.NET Core, and a console sink for logging to the console.

## Create a Logger Configuration:

In your **Program.cs** file, you can configure Serilog in the **CreateHostBuilder** method. Here's an example of how to configure a logger that logs to the console:

```plaintext
using Microsoft.AspNetCore.Hosting;
using Serilog;

public class Program
{
    public static void Main(string[] args)
    {
        var logger = new LoggerConfiguration()
            .WriteTo.Console()
            .CreateLogger
```

In this code, we configure Serilog to log to the console with **WriteTo.Console()** and set it as the default logger using **Log.Logger = logger;**.

## Use Serilog with ASP.NET Core Logging:

Now, to use Serilog for ASP.NET Core logging, you need to configure it in the **Startup.cs** file. Inside the **ConfigureServices** method, add the following code to replace the default ASP.NET Core logger with Serilog:

```plaintext
services.AddLogging(builder =>
{
    builder.AddSerilog();
});
``
```

## Use Logging in Your Application:

In your controllers or services, you can inject an **ILogger<T>** where **T** is the type you want to associate with the log entry. For example:

```plaintext
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

[ApiController]
[Route("api/[controller]")]
```

With this setup, Serilog will handle the logging, and you can use various log levels and structured logging features as needed.

Remember to customize the Serilog configuration to your specific needs, including configuring different sinks (e.g., file, database) and enrichers (e.g., including additional context information in log entries).


---

Original Source: https://www.mindstick.com/forum/160465/configure-serilog-with-webapplicationbuilder-in-dot-net-core-6

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
