serilog / serilog-extensions-hosting

Serilog logging for Microsoft.Extensions.Hosting
Apache License 2.0
140 stars 33 forks source link

Serilog.Extensions.Hosting Build status NuGet Version

Serilog logging for Microsoft.Extensions.Hosting. This package routes framework log messages through Serilog, so you can get information about the framework's internal operations written to the same Serilog sinks as your application events.

Versioning: This package tracks the versioning and target framework support of its Microsoft.Extensions.Hosting dependency. Most users should choose the version of Serilog.Extensions.Hosting that matches their application's target framework. I.e. if you're targeting .NET 7.x, choose a 7.x version of Serilog.Extensions.Hosting. If you're targeting .NET 8.x, choose an 8.x Serilog.Extensions.Hosting version, and so on.

Instructions

First, install the Serilog.Extensions.Hosting NuGet package into your app. You will need a way to view the log messages - Serilog.Sinks.Console writes these to the console; there are many more sinks available on NuGet.

dotnet add package Serilog.Extensions.Hosting
dotnet add package Serilog.Sinks.Console

Next, in your application's Program.cs file, configure Serilog first. A try/catch block will ensure any configuration issues are appropriately logged. Call AddSerilog() on the host application builder:

using Serilog;

Log.Logger = new LoggerConfiguration()
    .Enrich.FromLogContext()
    .WriteTo.Console()
    .CreateLogger();

try
{
    Log.Information("Starting host");

    var builder = Host.CreateApplicationBuilder(args);
    builder.Services.AddHostedService<PrintTimeService>();
    builder.Services.AddSerilog();

    var app = builder.Build();

    await app.RunAsync();
    return 0;
}
catch (Exception ex)
{
    Log.Fatal(ex, "Host terminated unexpectedly");
    return 1;
}
finally
{
    await Log.CloseAndFlushAsync();
}

Finally, clean up by removing the remaining "Logging" section from appsettings.json files (this can be replaced with Serilog configuration as shown in this example, if required)

That's it! You will see log output like:

[22:10:39 INF] Getting the motors running...
[22:10:39 INF] The current time is: 12/05/2018 10:10:39 +00:00

A more complete example, showing appsettings.json configuration, can be found in the sample project here.

Using the package

With Serilog.Extensions.Hosting installed and configured, you can write log messages directly through Serilog or any ILogger interface injected by .NET. All loggers will use the same underlying implementation, levels, and destinations.

Inline initialization

You can alternatively configure Serilog using a delegate as shown below:

    // dotnet add package Serilog.Settings.Configuration
builder.Services.AddSerilog((services, loggerConfiguration) => loggerConfiguration
    .ReadFrom.Configuration(builder.Configuration)
    .Enrich.FromLogContext()
    .WriteTo.Console())

This has the advantage of making builder's Configuration object available for configuration of the logger, but at the expense of ignoring Exceptions raised earlier in program startup.

If this method is used, Log.Logger is assigned implicitly, and closed when the app is shut down.