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
         }
       }
     ]
   }
Continue Reading

Fix for ‘’Npgsql.PostgresException: syntax error at or near ‘[‘”

We decided to change our database from SQL server to Postgres in our application. When I tried to apply the database migrations to the database it failed with a
Npgsql.PostgresException (0x80004005): 42601: syntax error at or near "[" error.
On further analyzing the issue I found that this issue occurs as the migrations were created on SQL Server and contain SQL Server-specific code.

To fix this issue, I
a. Removed the Migrations folder in your application.
b. Ran “Add-Migration Init” in the package manager console
c. Ran “Update-Database”, to flush the SQL Server predefined context.

Continue Reading