Serilog and .NET Core 2.1 HostBuilder Configuration
Try the new package now available in Serilog - https://github.com/serilog/serilog-extensions-hosting.
public static IHost BuildHost(string[] args) =>
new HostBuilder()
.ConfigureServices(services => services.AddSingleton<IHostedService, PrintTimeService>())
.UseSerilog() // <- Add this line
.Build();
Here is a sample that shows how to do this including using appsettings.json for configuring serilog and how to get the logging using ILogger without having to manually inject it as the marked answer shows and how you can also you IOptions:
public class Settings
{
public string Sample { get; set; }
}
public class Service : IHostedService
{
private readonly ILogger<Service> _logger;
private Settings _settings;
public Service(ILogger<Service> logger,
Settings settings)
{
_logger = logger;
_settings = settings;
}
public Task StartAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
class Program
{
static async Task Main(string[] args)
{
var host = new HostBuilder()
.ConfigureHostConfiguration(builder =>
{
builder.AddJsonFile("hostsettings.json", optional: true);
})
.ConfigureAppConfiguration((hostContext, builder) =>
{
builder.AddJsonFile("appsettings.json");
builder.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true);
})
.ConfigureLogging((hostContext, builder) =>
{
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(hostContext.Configuration).CreateLogger();
builder.AddConfiguration(hostContext.Configuration.GetSection("Logging"));
builder.AddSerilog(dispose: true);
})
.ConfigureServices((hostContext, services) =>
{
var settings = hostContext.Configuration.GetSection("Configuration").Get<Settings>();
services.AddSingleton(settings);
services.AddHostedService<Service>();
services.AddLogging();
services.AddOptions();
})
.Build();
using (host)
{
await host.StartAsync();
await host.WaitForShutdownAsync();
}
}
}
What I'm looking for is a way, from within the .ConfigureLogging() method to access a ServiceProvider instance so I can get the registered ILogger
You can access the ServiceProvider
from within the ConfigureLogging()
method via ILoggingBuilder.Services.BuildServiceProvider()
. Like so:
//...
private static async Task Main(string[] args)
{
var hostBuilder = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddSingleton<ILogger>(BuildLogger);
// other services here
})
.ConfigureLogging((hostContext, loggingBuilder) =>
loggingBuilder.AddSerilog(
loggingBuilder
.Services.BuildServiceProvider().GetRequiredService<ILogger>(),
dispose: true));
await hostBuilder.RunConsoleAsync();
}
...//