serilog net core 3 appsettings.json mssql sink code example

Example: serilog-in-aspnetcore-3 sql server configuration

public class Program
{
    public static IConfiguration Configuration { get; } = new ConfigurationBuilder()        .SetBasePath(Directory.GetCurrentDirectory())        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)        .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)        .Build();
    public static void Main(string[] args)
    {
        Log.Logger = new LoggerConfiguration()            .ReadFrom.Configuration(Configuration)            .CreateLogger();
        try
        {
            Log.Information("Getting the motors running...");

            BuildWebHost(args).Run();
        }
        catch (Exception ex)
        {
            Log.Fatal(ex, "Host terminated unexpectedly");
        }
        finally
        {
            Log.CloseAndFlush();
        }
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
               .UseStartup<Startup>()
               .UseConfiguration(Configuration)
               .UseSerilog()               .Build();
}

Tags:

Sql Example