Configure .NET Core to use Serilog

One of the first things I want to do after I dotnet new mvc or something like that, is switch the logging over to Serilog, and start logging to my local Seq server. I also want Serilog to take the place of the default console logger, but I still want to use Microsoft.Extensions.Logging.ILogger because, well, it’s there.

Here’s some generic configuration code to do it.

You’ll need these package references (version numbers destined to be out-of-date):

    <PackageReference Include="Serilog" Version="2.7.1" />
    <PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
    <PackageReference Include="Serilog.Settings.AppSettings" Version="2.1.2" />
    <PackageReference Include="Serilog.Settings.Configuration" Version="2.6.1" />
    <PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
    <PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" />

Then following the advice here, here, here and here.

Ultimately I end up this:

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseSerilog()
                .UseStartup<Startup>();

        public static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();

            Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(config)
                .CreateLogger();

            try
            {
                CreateWebHostBuilder(args).Build().Run();
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Host terminated unexpectedly");
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
    }

What’s important to note here is

{
  "Serilog": {
    "Using":  ["Serilog.Sinks.Console", "Serilog.Sinks.Seq"],
    "MinimumLevel": "Debug",
    "WriteTo": [
      { "Name": "Console" },
      { "Name": "Seq", "Args": { "serverUrl": "http://localhost:5341" } }
      
    ],
    "Enrich": ["FromLogContext", "WithMachineName", "WithThreadId"],
    "Destructure": [
      { "Name": "With", "Args": { "policy": "Sample.CustomPolicy, Sample" } },
      { "Name": "ToMaximumDepth", "Args": { "maximumDestructuringDepth": 4 } },
      { "Name": "ToMaximumStringLength", "Args": { "maximumStringLength": 100 } },
      { "Name": "ToMaximumCollectionCount", "Args": { "maximumCollectionCount": 10 } }
    ],
    "Properties": {
		"Application": "Sample"
    }
  },
  "AllowedHosts": "*"
}

Now I get logging to the console and to Seq, which is a good start.