Serilog is an alternative logging implementation that plugs into ASP.NET Core.
“Like many other libraries for .NET, Serilog provides diagnostic logging to files, the console, and elsewhere. It is easy to set up, has a clean API, and is portable between recent .NET platforms.”
Initialization with top-level try/catch block in Program.cs
Doing it this way catches any start up errors and closes high level streams with CloseAndFlush
Log.x static instances can also be used through-out the application however Im a fan of rather using dependancy injection so the coupling is to the ILogger interface.
Remove LoggerConfiguration instantiation from Main in Program.cs and rather use UseSerilog() in CreateHostBuilder(). This comes from Serilog.Settings.Configuration
publicstatic IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration(ConfigureAppLogging) // Serilog configuration is done in `ConfigureAppLogging` and not the `webBuilder` .UseSerilog() // Sets Serilog as the logging provider. .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); // You dont have to do any manual DI in `Startup`, `UseSerilog()` above creates an instance of `ILogger<T>` for you });
4.1 The Serilog configuration can then either be read from appSettings.json in the Serilog: key/value
1 2 3 4 5 6
privatestaticvoidConfigureAppLogging(IConfigurationBuilder config) { Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(config.Build()) .CreateLogger(); }
public IActionResult Index() { _logger.LogInformation("FOO INDEX RUN!"); return View(); } }
Exclude Logging
Tools like kubernetes need to know if the container is alive, the simplest way to do this is though a ping endpoint that doesnt have any authentication.