Configure error logging using Serilog.Sinks.Postgres and appSettings.json

Install nuget packages
Serilog.Sinks.PostgreSQL
Microsoft.Extensions.Configuration.Json

Program.cs:

public class Program
   {
     public static void Main(string[] args)
     {
       var path = Directory.GetCurrentDirectory();
       var environmentName =Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";
      var configuration = new ConfigurationBuilder()
         .SetBasePath(path)
         .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
         .AddJsonFile($"appsettings.{environmentName}.json", optional: true, reloadOnChange: true)
         .AddEnvironmentVariables()
         .Build();
       Log.Logger = new LoggerConfiguration().
         Enrich.FromLogContext().
         ReadFrom.Configuration(configuration).
         CreateLogger();
      try
       {
         var iWebHost = CreateWebHostBuilder(args).Build();
         Log.Information("Application starting");
         iWebHost.Run();
       }
       catch (Exception exception)
       {
         Log.Error(exception.ToString());
       }
       finally
       {
         Log.CloseAndFlush();
       }
     }
    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .UseStartup()
        .UseSerilog();
   }

appsettings.json:

"Serilog": {
     "Using": [ "Serilog.Sinks.PostgreSQL" ],
     "MinimumLevel": "Warning",
     "WriteTo": [
       {
         "Name": "PostgreSQL",
         "Args": {
           "connectionString": "",
           "tableName": "Logs",
           "needAutoCreateTable": true,
           "batchPostingLimit": 1
         }
       }
     ]
   }

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.