ASP .NET Core Logging with Serilog

The de facto loggers in the .NET space have historically been log4net and NLog. Along comes Serilog and it has a more metadata"y" approach that they describe as structured events. Serilog fits in extremely well with modern aggregated logging services like Elastisearch, Loggly, etc...

Logging Goals:

  • Upon application start, log an Informational started message before doing any other work
  • Any process exceptions should be logged as Fatal
  • Log to a file and keep the last 30 days of logs
  • Production logging should be set to Informational
// Install-Package Serilog.AspNetCore
// Install-Package Serilog.Sinks.Console
// Install-Package Serilog.Sinks.RollingFile
// Install-Package Serilog.Sinks.Async
// This code is slightly modified from

public class Program
    public static int Main(string[] args)
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) // Warning level will exclude individual requests
            // RollingFile defaults to 31 days and a max file size of 1GB
            .WriteTo.Async(a => a.RollingFile("logs/myapp-{Date}.log", LogEventLevel.Information)) // Good idea to wrap any file sinks in an Async sink

            Log.Information("Starting web host");

            var host = new WebHostBuilder()
                .UseSerilog()  // Sets Serilog as the logging provider


            return 0;
        catch (Exception ex)
            Log.Fatal(ex, "Host terminated unexpectedly");
            return 1;