HaemmerElectronics.SeppPenner.SerilogSinkForPostgreSQL 1.0.2

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package HaemmerElectronics.SeppPenner.SerilogSinkForPostgreSQL --version 1.0.2
NuGet\Install-Package HaemmerElectronics.SeppPenner.SerilogSinkForPostgreSQL -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HaemmerElectronics.SeppPenner.SerilogSinkForPostgreSQL" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HaemmerElectronics.SeppPenner.SerilogSinkForPostgreSQL --version 1.0.2
#r "nuget: HaemmerElectronics.SeppPenner.SerilogSinkForPostgreSQL, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install HaemmerElectronics.SeppPenner.SerilogSinkForPostgreSQL as a Cake Addin
#addin nuget:?package=HaemmerElectronics.SeppPenner.SerilogSinkForPostgreSQL&version=1.0.2

// Install HaemmerElectronics.SeppPenner.SerilogSinkForPostgreSQL as a Cake Tool
#tool nuget:?package=HaemmerElectronics.SeppPenner.SerilogSinkForPostgreSQL&version=1.0.2

SerilogSinkForPostgreSQL

SerilogSinkForPostgreSQL is a library to save logging information from Serilog to PostgreSQL. The assembly was written and tested in .Net Framework 4.8 and .Net Standard 2.0.

Build status GitHub issues GitHub forks GitHub stars GitHub license Nuget NuGet Downloads Known Vulnerabilities

Available for

  • NetFramework 4.5
  • NetFramework 4.6
  • NetFramework 4.6.2
  • NetFramework 4.7
  • NetFramework 4.7.2
  • NetFramework 4.8
  • NetStandard 2.0

Basic usage:

string connectionString = "User ID=serilog;Password=serilog;Host=localhost;Port=5432;Database=Logs";

string tableName = "logs";

IDictionary<string, ColumnWriterBase> columnWriters = new Dictionary<string, ColumnWriterBase>
{
    { "message", new RenderedMessageColumnWriter(NpgsqlDbType.Text) },
    { "message_template", new MessageTemplateColumnWriter(NpgsqlDbType.Text) },
    { "level", new LevelColumnWriter(true, NpgsqlDbType.Varchar) },
    { "raise_date", new TimestampColumnWriter(NpgsqlDbType.TimestampTz) },
    { "exception", new ExceptionColumnWriter(NpgsqlDbType.Text) },
    { "properties", new LogEventSerializedColumnWriter(NpgsqlDbType.Jsonb) },
    { "props_test", new PropertiesColumnWriter(NpgsqlDbType.Jsonb) },
    { "machine_name", new SinglePropertyColumnWriter("MachineName", PropertyWriteMethod.ToString, NpgsqlDbType.Text, "l") }
};

var logger = new LoggerConfiguration()
	.WriteTo.PostgreSQL(connectionString, tableName, columnWriters)
	.CreateLogger();

The project can be found on nuget.

Configuration options:

Parameter Meaning Example Default value
connectionString The connection string to connect to the PostgreSQL database. "User ID=serilog;Password=serilog;Host=localhost;Port=5432;Database=Logs" None, is mandatory.
tableName The table name to write the data to. Is case-sensitive! "logs" None, is mandatory.
period The time to wait between checking for event batches. period: new TimeSpan(0, 0, 20) 00:00:05
formatProvider The IFormatProvider to use. Check https://docs.microsoft.com/en-us/dotnet/api/system.iformatprovider?view=netframework-4.8|`null`|
columnOptions The column options to do. See the examples under the Full example section below. null
batchSizeLimit The maximum number of events to include in a single batch. batchSizeLimit: 40 30
useCopy Enables the copy command to allow batch inserting instead of multiple INSERT commands. useCopy: true true
schemaName The schema in which the table should be created. schemaName: "Logs" string.Empty which defaults to the PostgreSQL public schema.
needAutoCreateTable Specifies whether the table should be auto-created if it does not already exist or not. needAutoCreateTable: true false
queueLimit Maximum number of events in the queue. queueLimit: 3000 int.MaxValue or 2147483647

Full example

string connectionString = "User ID=serilog;Password=serilog;Host=localhost;Port=5432;Database=Logs";

string tableName = "logs";

IDictionary<string, ColumnWriterBase> columnOptions = new Dictionary<string, ColumnWriterBase>
{
    { "message", new RenderedMessageColumnWriter(NpgsqlDbType.Text) },
    { "message_template", new MessageTemplateColumnWriter(NpgsqlDbType.Text) },
    { "level", new LevelColumnWriter(true, NpgsqlDbType.Varchar) },
    { "raise_date", new TimestampColumnWriter(NpgsqlDbType.TimestampTz) },
    { "exception", new ExceptionColumnWriter(NpgsqlDbType.Text) },
    { "properties", new LogEventSerializedColumnWriter(NpgsqlDbType.Jsonb) },
    { "props_test", new PropertiesColumnWriter(NpgsqlDbType.Jsonb) },
    { "machine_name", new SinglePropertyColumnWriter("MachineName", PropertyWriteMethod.ToString, NpgsqlDbType.Text, "l") }
};

var logger = new LoggerConfiguration()
	.WriteTo.PostgreSQL(connectionString, tableName, columnOptions, needAutoCreateTable: true, schemaName: "LoggingSchema", useCopy: true, queueLimit: 3000, batchSizeLimit: 40, period: new TimeSpan(0, 0, 10), formatProvider: null)
	.CreateLogger();

Adjusting column sizes

You can change column sizes by setting the values in the TableCreator class:

// Sets size of all BIT and BIT VARYING columns to 20
TableCreator.DefaultBitColumnsLength = 20;

// Sets size of all CHAR columns to 30
TableCreator.DefaultCharColumnsLength = 30;

// Sets size of all VARCHAR columns to 50
TableCreator.DefaultVarcharColumnsLength = 50;

Upper or lower case table or column names

Table or column names are always case-sensitive!

Further information:

This project is a fork of https://github.com/b00ted/serilog-sinks-postgresql but is maintained. Do not hesitate to create issues or pull requests.

Change history

  • Version 1.0.2.0 (2019-05-13) : Updated documentation, fixed some tests.
  • Version 1.0.1.0 (2019-05-08) : Updated documentation, added documentation to the nuget package and all classes, added option to allow upper case table and column names. Simplified building and packing scripts. Added support for NetFramework 4.6, NetFramework 4.6.2, NetFramework 4.7 and NetFramework 4.8.
  • Version 1.0.0.0 (2019-02-22) : 1.0 release.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 is compatible.  net461 was computed.  net462 is compatible.  net463 was computed.  net47 is compatible.  net471 was computed.  net472 is compatible.  net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.3.7 95,640 8/29/2021
3.3.6 4,467 8/9/2021
3.3.5 1,262 7/25/2021
3.3.4 30,016 6/4/2021
3.3.3 9,763 4/29/2021
3.3.2 27,100 3/4/2021
3.3.1 2,639 3/4/2021
3.3.0 1,798 2/21/2021
3.2.4 123 2/21/2021
3.2.3 5,870 1/14/2021
3.2.2 1,695 1/4/2021
3.2.1 448 1/3/2021
3.2.0 497 12/26/2020
3.1.0 1,090 11/16/2020
3.0.0 2,302 11/11/2020
2.5.0 526 10/13/2020
2.4.1 190 10/13/2020
2.4.0 4,235 9/6/2020
2.3.1 259 9/6/2020
2.3.0 548 9/6/2020
2.2.0 6,948 7/25/2020
2.1.0 130,289 6/30/2020
2.0.0 1,154 6/16/2020
1.9.0 525 6/16/2020
1.8.1 533 6/5/2020
1.7.0 7,472 5/10/2020
1.6.0 8,796 3/26/2020
1.5.0 10,936 2/9/2020
1.0.4 12,767 11/8/2019
1.0.3 1,009 6/23/2019
1.0.2 669 5/13/2019
1.0.1 620 5/12/2019
1.0.0 598 2/22/2019

Version 1.0.2.0 (2019-05-13): Updated documentation, fixed some tests.
Version 1.0.1.0 (2019-02-23): Updated documentation, added documentation to the nuget package and all classes, added option to allow upper case table and column names.
Version 1.0.0.0 (2019-02-22): 1.0 release.