4 Steps to Setup Serilog in ASP.NET Core 7

A third-party, open-source structured logging library is called Serilog. In addition to other places, it offers diagnostic logging to files and the console. It is portable between recent.NET platforms, straightforward to set up, and provides a clean API. Serilog has become a well-liked logging library in the ASP.NET Core framework because of its adaptability, extensibility, and simplicity.

Here are steps by steps to setup Serilog in your ASP.NET Core 7 application:

1. Install the Serilog NuGet Package

Install the following Nuget package, either via package manager console,

Install-Package Serilog.AspNetCore

Or, via Nuget Package manager.

2. Register Serilog

Next, open program.cs and register serilog.

// Add services to the container.
builder.Host.UseSerilog((context, configuration) => configuration.ReadFrom.Configuration(context.Configuration));

Also, register the pipeline.


3. Configure the settings in appsetting.json

Open appsetting.json file and add following code.

 "Serilog": {
    "Using": [ "Serilog.Sinks.File", "Serilog.Sinks.Console" ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
    "WriteTo": [
        "Name": "Console"
        "Name": "File",
        "Args": {
          "path": "/logs/log-.txt",
          "rollOnFileSizeLimit": true,
          "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter,Serilog.Formatting.Compact",
          "rollingInterval": "Day"
    "Enrich": [ "FromLogContext", "WithThreadId", "WithMachineName" ]

Here, we specify that logs should be added to both the dotnet console and a .txt file that will be generated in the c:logs folder. Where to log is specified in the line "Using": ["Serilog.Sinks.File", "Serilog.Sinks.Console"].

You can also write to .json in place of .txt. Change the file extension in the code above to do that.

4. Test it!

Open your controller and start logging. Like,

public IEnumerable<WeatherForecast> Get()
    _logger.LogInformation("WeatherForecast get method Starting.");
    return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
        TemperatureC = Random.Shared.Next(-20, 55),
        Summary = Summaries[Random.Shared.Next(Summaries.Length)]

Make sure, logger in injected in your controller. Like,

private readonly ILogger<WeatherForecastController> _logger;

public WeatherForecastController(ILogger<WeatherForecastController> logger)
    _logger = logger;

Run your app now and you should see these.


Now, you have learned how to setup Serilog in Asp.net core 7. We will back with other interesting tutorial.

Are you looking for an affordable and reliable Asp.net core hosting solution?

Get your ASP.NET hosting as low as $1.00/month with ASPHostPortal. Our fully featured hosting already includes

  • Easy setup
  • 24/7/365 technical support
  • Top level speed and security
  • Super cache server performance to increase your website speed
  • Top 9 data centers across the world that you can choose.

Related Posts

Leave a Reply

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