serilog-mssql /serilog-sinks-mssqlserver
A Serilog sink that writes events to Microsoft SQL Server and Azure SQL
★ 300⑂ 145👁 19⑃ 383● 307▤ 48
300
stars
145
forks
19
watchers
383
prs
307
issues
48
releases
README
Serilog.Sinks.MSSqlServer !Build & Test
A Serilog sink that writes events to Microsoft SQL Server. This sink will write the log event data to a table and can optionally also store the properties inside an XML or JSON column so they can be queried. Important properties can also be written to their own separate columns.
Package - Serilog.Sinks.MSSqlServer
| Minimum Platforms - .NET Framework 4.6.2, .NET 8.0, .NET Standard 2.0
Topics
Quick Start
Sink Configuration
Audit Sink Configuration
Table Definition
MSSqlServerSinkOptions Object
ColumnOptions Object
SqlColumn Objects
Standard Columns
Custom Property Columns
External Configuration Syntax
Troubleshooting
Querying Property Data
Breaking Changes
Deprecated Features
Quick Start
The most basic minimalistic sink initialization is done like this.
csharp
Log.Logger = new LoggerConfiguration()
.WriteTo
.MSSqlServer(
connectionString: "Server=localhost;Database=LogDb;Integrated Security=SSPI;",
sinkOptions: new MSSqlServerSinkOptions { TableName = "LogEvents" })
.CreateLogger();
Sample Programs
There is a set of small and simple sample programs provided with the source code in the sample directory. They demonstrate different ways to initialize the sink by code and configuration for different target frameworks.
Sink Configuration
The sink can be configured completely through code, by using configuration files (or other types of configuration providers), a combination of both, or by using the various Serilog configuration packages. There are two configuration considerations: configuring the sink itself, and configuring the table used by the sink. The sink is configured with a typical Serilog WriteTo configuration method (or AuditTo, or similar variations). Settings for the sink are configured using a MSSqlServerSinkOptions object passed to the configuration method. The table is configured with an optional ColumnOptions object passed to the configuration method.
All sink configuration methods accept the following arguments, though not necessarily in this order. Use of named arguments is strongly recommended. Some platform targets have additional arguments.
connectionString
sinkOptions
columnOptions
restrictedToMinimumLevel
formatProvider
logEventFormatter
Basic Arguments
At minimum, connectionString and MSSqlServerSinkOptions.TableName are required. If you are using an external configuration source such as an XML file or JSON file, you can use a named connection string instead of providing the full "raw" connection string.
All properties in the MSSqlServerSinkOptions object are discussed in the MSSqlServerSinkOptions Object topic.
Table configuration with the optional ColumnOptions object is a lengthy subject discussed in the ColumnOptions Object topic and other related topics.
Like other sinks, restrictedToMinimumLevel controls the LogEventLevel messages that are processed by this sink. This parameter is ignored if the property LevelSTop issues
#318Default value for additional sql columns in the table.5 comments · 2020-10-10#633does not support Command Timeout=30 in the connection string3 comments · 2025-07-22#477MSSQLSink LogEvent column to be varbinary(max)1 comments · 2023-07-13
Discussions
#657Why not opinionate / preset in 16 or 20 as DataLength for LevelColumn2 comments · 2025-12-01#652Automatically truncate message column1 comments · 2025-10-28#638Best approach to handle large Serilog MSSQL log tables (indexing issues)1 comments · 2025-10-05#635Log messages not always saved to db - (Maybe flushing issue?)1 comments · 2025-09-19#626package V8.2.1 missing from nuget gallery1 comments · 2025-07-11#595Release 8.0.0 with update to .NET 80 comments · 2024-10-31#587Plan for 7.0.2 release2 comments · 2024-10-17#575Additional Column - Static Value1 comments · 2024-09-23
Latest release
v10.0.0 v10.0.0 2026-05-22Updated SqlClient from 6 to 7 including breaking changes around Azure Entra Id authentication. (thanks to @david-brink-talogy for Azure auth testing) Updated minimum SDK to 10.0.7 because of CVEs in .NET Replaced FluentAssertions with AwesomeAssertions for license reasons Updated other dependencies