Many of you come to my site to read the post Step by step: Serilog with ASP.NET Core which I wrote in 2016 and is completely out of date, so with this post I will show you how to setup Serilog to work with your ASP.NET Core 2.2 applications.

Create an ASP.NET Core project


1    md aspnet.serilog.sample
2    cd aspnet.serilog.sample
3    dotnet new mvc

Add the following dependencies to your project


1    dotnet add package Serilog.AspNetCore
2    dotnet add package Serilog.Extensions.Logging
3    dotnet add package Serilog.Sinks.ColoredConsole

Change your Program.cs file to look like the following


 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using System.Linq;
 5using System.Threading.Tasks;
 6using Microsoft.AspNetCore;
 7using Microsoft.AspNetCore.Hosting;
 8using Microsoft.Extensions.Configuration;
 9using Microsoft.Extensions.Logging;
10using Serilog;
11using Serilog.Core;
12using Serilog.Events;
13
14namespace aspnet.serilog.sample
15{
16    public class Program
17    {
18        public static void Main(string[] args)
19        {
20            Log.Logger = new LoggerConfiguration()
21               .Enrich.FromLogContext()
22               .MinimumLevel.Debug()
23               .WriteTo.ColoredConsole(
24                   LogEventLevel.Verbose,
25                   "{NewLine}{Timestamp:HH:mm:ss} [{Level}] ({CorrelationToken}) {Message}{NewLine}{Exception}")
26                   .CreateLogger();
27
28            try
29            {
30                CreateWebHostBuilder(args).Build().Run();
31            }
32            finally
33            {
34                Log.CloseAndFlush();
35            }
36        }
37
38        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
39            WebHost.CreateDefaultBuilder(args)
40                .UseSerilog()
41                .UseStartup<Startup>();
42    }
43}

Inject the logger to your services or controllers


Change the home controller and log some actions:

 1using System;
 2using System.Collections.Generic;
 3using System.Diagnostics;
 4using System.Linq;
 5using System.Threading.Tasks;
 6using Microsoft.AspNetCore.Mvc;
 7using aspnet.serilog.sample.Models;
 8using Microsoft.Extensions.Logging;
 9
10namespace aspnet.serilog.sample.Controllers
11{
12    public class HomeController : Controller
13    {
14        ILogger<HomeController> logger;
15
16        public HomeController(ILogger<HomeController> logger)
17        {
18            this.logger = logger;
19        }
20
21        public IActionResult Index()
22        {
23            this.logger.LogDebug("Index was called");
24            return View();
25        }
26
27        public IActionResult Privacy()
28        {
29            return View();
30        }
31
32        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
33        public IActionResult Error()
34        {
35            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
36        }
37    }
38}

Download the complete code here

Hope it helps!

Learn More

Fundamentals: Logging in .NET Core and ASP.NET Core