Customize Serilog configuration after ReadFrom.Configuration

453 Views Asked by At

I have a ASP.NET Core 5 Web API application in which I am using Serilog like this:

public static class Program
{
    public static void Main(string[] args)
    {
        Log.Logger = new LoggerConfiguration()
            .WriteTo.Console()
            .CreateBootstrapLogger(); 

        try
        {
            CreateHostBuilder(args).Build().Run();
        }

        // ...

    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .UseSerilog((context, services, configuration) => configuration
                .WriteTo.Console()
                .ReadFrom.Configuration(context.Configuration))
            .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
    }

I want to customize some configuration options before actually applying them.

For instance, in Elasticsearch Sink some options are of type Func<...> which I can't define in appsettings.json nor in context.Configuration directly.

I Know I can do all configuration in code, but I want the benefit of setting some options from appsettings.json and some others from code.

Is that possible?

If not, how do I configure a delegate type option (Func, Action) from appsettings.json?

0

There are 0 best solutions below